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

find/replace with charBuffer
How does Integer.parseInt works
java.io.IOException: Permission denied with Xuggle
String formatting remove everything after </table> tag
How to log data to a JTextArea (or JTextPane) from a thread that doesn't have a swing component init?
Android - sqlite delete all
welcome-file in web.xml with spring not working?
Glassfish & JMS: Why do published messages not arrive at subscribers?
SWT Use Composite from File
Remove space around title in TitledBorder
JUnit and JFileChoosers
f:ajax Unable To See My Conversation
Remove a part of string via first and last character?
Studying resource context in Java projects
How do I play two sounds at once?
Is there a way I can run TestNG tests against multiple interface implementations (DataSources)?

Categories

HOME
asp.net-core
proxy
server
tizen
google-play
cplex
react-router
youtube-dl
routes
jsp-tags
adb
getelementsbytagname
sqlite3
v8
mvvmcross
datastax-java-driver
kibana-4
rascal
dtrace
hapi
systemc
crystal-reports-2008
firefox-webextensions
one-hot-encoding
facebook-instant-articles
claims-based-identity
sparse-matrix
intel-pin
fopen
maquette
procdump
delicious-api
binary-data
google-cloud-endpoints-v2
airconsole
copying
framemaker
y86
wpf-controls
host
plsql-psp
devextreme
withings
starteam
sqlite2
grid.mvc
netcdf4
s
logparser
hls.js
pebble-watch
veracode
gabor-filter
drupal-6
colorama
windows-iot-core-10
knpmenubundle
jspdf-autotable
django-scheduler
cubes
medium.com
wdf
firmata
elements
fody
xml-attribute
player
gridview-sorting
clang-static-analyzer
pagedlist
phpcas
xna-4.0
wso2cloud
cannon.js
python-3.2
wyam
ibaction
sailfish-os
block-device
ng-animate
knuth
titanium-modules
yorick
seaside
jboss-weld
didselectrowatindexpath
algebraic-data-types
multiprocessor
dotnetnuke-5
qt-faststart
pushbackinputstream
spring-validator
tinn-r
browser-detection
runas
parametric-equations
uiviewanimation-curve
vdsp
coda-slider
icanhaz.js
virtual-functions
putchar
post-build

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