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

Execute linux command in java and display output to html table
Java Annotations javaArgs and LiteralArguments are deprecated
NoSuchMethodError while parsing xsd to generate classes using JAXB
Hibernate using sql call for each row to fetch relationship - instead of aggregating to one IN clause
How do I make a local variable increase consistently in a recursive function having more than one calls?
Intellij IDEA : Spark code running results in java.lang.VerifyError
JavaParser comparison
How To use setOnItemClickListner multiple times in a ListView?
NullPointer exception in a Spring Rest Controller test - Java
Most suitable Java Collection for duplicate elements
Android Custom SeekBar with custom circles
Jython--Exception in thread “main” IOError: (2, 'File not found ')
black activity giving error android studio
Spring Boot running java and web content on different ports in a single application
List in Android: Rename duplicated items
unsupported major minor version 51.0 error in ubuntu [duplicate]

Categories

HOME
vim
keras
drivers
yarn
networkx
u-sql
windows-phone-7
uitypeeditor
google-static-maps
invantive-sql
jtextfield
kudan
dxf
spark-jobserver
react-css-modules
fgetcsv
fluentvalidation
minitab
vision
preg-match
http-get
google-api-nodejs-client
bytecode-manipulation
automake
service-discovery
xenforo
temporary-files
user-controls
import-from-excel
checkboxlist
modelmapper
flashair
eclipse-gef
thin
dotcover
osx-mavericks
appcompat
filepicker
blogengine.net
gabor-filter
git-diff
tropo
drupal-6
darcs
carrot
directory-structure
capacity
memory-alignment
imanage
bind9
azure-virtual-network
file-writing
plottable.js
disque
hexdump
livequery
pyke
python-3.2
coveralls
dukescript
sframe
mono-embedding
sankey-diagram
mmc
system32
p2
directoryservices
kineticjs
python-green
dia
typekit
oxwall
tt-news
javafx-webengine
asp.net-web-api-odata
jboss-weld
message-driven-bean
imdbpy
oam
dotnetnuke-5
buster.js
back-stack
factory-method
selected
frameset
windows-phone-7.1.1
jquery-ui-layout
fireworks
gnu-prolog
window-management
gnustep
xdomainrequest
invite
actionview
visitor-statistic
digest-authentication
gallio
ncqrs
firefox4
anti-piracy
premature-optimization
aquaticprime
spec#
genealogy
misv

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