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

Starting one Spring Boot Application by using SoapUITestCaseRunner?
Can someone please clarify #RequestBody
Where do Grails domain classes get their query methods from?
Spring repository in Swing
Setting value without a setter method in java class [duplicate]
JSOUP Redirect Script
Change number of split
Can´t create entity in MongoDB
Log4j2 LogManager.getLogger() with Spring
How to add information to existing text file in java
Logging Features/Functions Used In an Application
Hibernate OneToOne BiDirectional Optional Relationship: Works when inserted without optional object, Breaks when updated with new optional object
getDocument() constantly returns a null value
How to capture requestor IP address in play java framework?
Why is the bottom of my fragment not displaying?
Java Android Studio libGDX Api 16 Crash

Categories

HOME
xamarin
variables
atom-editor
server
alfresco
getelementsbytagname
yahoo-oauth
acquia
paradox
gnupg
numeral.js
mapserver
messages
visual-studio-cordova
try-catch
angular-ui
interop
ml
autoconf
google-search-api
emgucv
dynamic-featured-image
status
brunch
maxmind
elasticsearch-ruby
file-rename
socialengine
twilio-api
catel
ghost4j
http-referer
mapbox-gl
jmonkeyengine
stacked
dosbox
jquery-bootgrid
hockeyapp
uft-api
android-fingerprint-api
elasticsearch-plugin
greenrobot-eventbus
dism
grid.mvc
turbogears
strptime
jedis
promela
dandelion
powercli
brightcove
knockout-components
windows-mobile-6.5
dynamics-sl
parallel-data-warehouse
xcb
setuptools
passport-google-oauth
rotativa
xml-attribute
autorest
react-native-listview
qgraphicsview
uid
msys
lemon
t4mvc
metaclass
retina
composite
oberon
p2
asp.net-dynamic-data
markers
rtmfp
monomac
gnu-smalltalk
soundtouch
jplaton
android-screen-support
libstdc++
pysimplesoap
dsn
cascalog
pendrive
hirefire
blackberry-playbook
coredump
coercion
celltable
sortable-tables
microsoft-virtualization
paul-graham





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