java


Use a non static function in a static one [duplicate]


This question already has an answer here:
Non-static variable cannot be referenced from a static context
10 answers
So in my Mainactivity.java, I have some buttons that change the value of a. It can be any number (I only use 1 and 2 now, but I'll add more).
And in my DummyContent.java, I have these function:
public static void setNumber(int number){
a = number;
}
private static int getNumber() {
return a;
}
private static DummyItem createDummyItem(int position) {
number = getNumber();
if (number == 1) {
//Do stuff
} else if (number == 2) {
//Do something else
} else {
//Report an error
}
}
And then in MainActivity.java I have:
DummyContent.setNumber(1);
It should be working, but it isn't. It is executing the else section.
Do you guys know a way to fix this? (Keep in mind that I am not very experienced.)
I cannot post an answer, so I''l put it here.
I solved my question with this code:
public static void setNumber(int number){
a = number;
}
private static int getNumber() {
if (a == null) {
a = 1;
}
return a;
}
private static DummyItem createDummyItem(int position) {
DummyItem item;
int number = getnumber();
if (number == 1) {
//Excecute some script
} else if (number == 2) {
//Execute some script
} else {
//Log the error
}
}
And then in the other classes, you'd want to put this:
DummyContent.setNumber(1); //Can be any integer.
Make the variable a static then.
Answer to updated question:
number = getNumber();
this should be:
int number = getNumber();

Related Links

Finishing details on a JUnit 4 dynamic suite
How do I copy a two dimensional array of strings?
In Java, what is a shallow copy?
Would this cause Garbage Collection issues
Should I use a Listener or Observer?
Java Array HashCode implementation
Java Regex Grouping Problem
Retrieving format string from Format object
What does the return keyword do in a void method in Java?
ThreadLocal Resource Leak and WeakReference
Mirth: calling an SSL SOAP web service with a client certificate
JAX-WS Exception
How can you alter the margins in Nimbus Look and Feel
jsp get ip address
How to display japanese characters in JTextArea
Java composition question

Categories

HOME
testng
vbscript
atom-editor
sd-card
adb
sharepoint-designer
frameworks
pivotal-cloud-foundry
flyway4
paradox
applepay
elasticsearch-hadoop
row
timeout
try-catch
telephony
jqwidget
textfield
ml
web-sql
accessor
wkwebview
dcevm
one-to-many
buildbot
fish
spring-mybatis
minitab
assistant
replaceall
bluestacks
stormpath
junit5
google-cloud-endpoints-v2
contact-form
uiswipegesturerecognizer
mapbox-gl
file-format
qwerty
xcode-extension
fedex
google-closure
node-gyp
android-mediaprojection
convertapi
sas-jmp
websphere-mq-fte
galleria
komodoedit
tizen-native-app
nsarray
windows-iot-core-10
mcafee
bind9
watchconnectivity
jquery-filter
ableton-live
url-pattern
rotativa
bonobo
disque
sdf
bluemix-app-scan
oauth2client
goose
asp.net5
phpcas
eclipse-clp
simple-framework
sailfish-os
sniffer
facebook-graph-api-v2.4
socketexception
mesa
hippomocks
dealloc
bundles
stxxl
imdbpy
runtime.exec
soundtouch
dotnetnuke-5
back-stack
jquery-mobile-dialog
random-seed
angularjs-controller
sublist
isnullorempty
funscript
frameset
uiviewanimation-curve
gnu-prolog
hamachi
f#-powerpack
xdomainrequest
invite
yui-datatable
disclosure
coda-slider
icanhaz.js
rescale
database-management
xetex
simpletest
temporal-database

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App