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

How do I fix Javadoc that no longer autofills/autocompletes?
Advice wanted on a complex structure in java (DAO and Service Layer linking/coupling)
Iterate through binary search tree to find all leaves
Not allowed to include a default constructor - Java Basics
Grabbing ints from a string in java
How to add elements to the body of the Http request in android
How to implement Comparable class
Does adding two files in this way?
Java Compare two Arrays of Images / Buttons on GridLayout Panel
Winpcap packet forwarding. Java wrapper
Workaround for this?
whats the different between when i create object from map and hashmap [duplicate]
Access to array list many times
SpringMVC REST Web service
How to break up an Android activity in multiple files
Java Calendar, acts differently OSX Windows

Categories

HOME
vbscript
atom-editor
gerrit
routes
youtube-api-v3
frameworks
echarts
opengl-es-2.0
convolution
swagger-ui
bs4
append
node-notifier
cross-validation
dacpac
kibana-4
postgres-xl
windows-phone-7
workload-scheduler
after-effects
tostring
pc
reactcsstransitiongroup
firefox-webextensions
clickonce
chromium-embedded
angular2-aot
underflow
kannel
ejabberd-module
wtx
windows-server-2000
yadcf
usbserial
form-data
catalog
overriding
suricata
vxworks
xcode-extension
libvpx
dynamics-crm-2013
occlusion
impersonation
splice
import-from-excel
termination
dds
fancybox-2
diagnostics
sybase-asa
r-forge
memory-alignment
mako
blacklist
crosswalk-runtime
jwplayer7
lua-5.1
ready-api
suffix-tree
two-factor-authentication
wapiti
srand
r-tree
execute
eclipse-clp
appfabric-cache
unity3d-gui
facebook-graph-api-v2.4
preferences
project-planning
mechanize-ruby
dir
soundtouch
dbconnection
http-unit
jquery-knob
quickdialog
cascalog
nsmanagedobject
amazon-appstore
stage
data-loss
gcj
coercion
horizontal-accordion
pyinotify
gdlib
xfbml
telerik-scheduler
lpeg
forums
fixed-width
mdac

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