Accessing static field

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

rossum said:
....


Still waiting on some sort of code example....
public class Example {

static class MyClass {
static int myStatic = 42;
}

public static void main(String[] args) {

MyClass mc = new MyClass();

System.out.println("Static class variable = " + mc.myStatic);
System.out.println("Static class variable = " + MyClass.myStatic);
}
}

The first println gives the symptoms the OP describes: "Accessing
static field ..." Clicking on the lightbulb icon gives two oprtions:

1 Replace with class reference.
2 Suppress Warning - static-access.

On the general principle that suppressing warnings is a BAD THING,
without very good reasons to do so, I would suggest that the first of
the two options is the better choice.

rossum

Thanks - I did not realise I could click on the light bulb!

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
K

Kevin McMurtrie

Dirk Bruere at NeoPax said:
Netbeans throws up this warning because I have a class that contains
static data fields. OK to ignore, or am I missing something here?

Your code sample is messed up but I'm guessing you're accessing a static
through an instance variable. That's being resolved at compile time,
whereas a non-static would be resolved at run time. It's a style and
readability problem because the value of the instance variable is
meaningless. It could be null and still work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Official Java Classes 10
Can an Applet beep? 4
ListModel name 10
Sorting a JList 4
JMF? 21
File over network timeout 3
Delay 2
Free keyboard applet 5

Members online

No members online now.

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top