Accessing static field

  • Thread starter Dirk Bruere at NeoPax
  • Start date
Q

Qu0ll

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

What does the warning say exactly? I get no such warnings.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
M

markspace

Dirk 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?


Probably not OK to ignore. Can you be more specific? How and where are
you accessing the field? At minimum you're making a stylistic error.
At worst your program won't compile. Hard to say without an SSCCE.
 
D

Dirk Bruere at NeoPax

markspace said:
Probably not OK to ignore. Can you be more specific? How and where are
you accessing the field? At minimum you're making a stylistic error. At
worst your program won't compile. Hard to say without an SSCCE.

Sorry, I get the little triangle and lightbulb next to the code - not
compiler warnings
Program compiles and runs OK, as expected.

--
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
 
Q

Qu0ll

Dirk Bruere at NeoPax said:
Sorry, I get the little triangle and lightbulb next to the code - not
compiler warnings
Program compiles and runs OK, as expected.

If you move the mouse over the icon it should give you a bit more info about
why it is there.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
M

markspace

Dirk said:
Sorry, I get the little triangle and lightbulb next to the code - not
compiler warnings
Program compiles and runs OK, as expected.


.....


Still waiting on some sort of code example....
 
D

Dirk Bruere at NeoPax

Qu0ll said:
If you move the mouse over the icon it should give you a bit more info
about why it is there.

Just "Accessing static field"
Code example:

public class MediaLists
{
public static DefaultListModel dvdListModel = new defaultListModel();
....

public void doStuffTo_dvdListModel(){}

}

MediaLists MedialList;
MediaList.doStuffTo_dvdListModel(); //Triangle and
lightbulb with //"Accessing static field" here

--
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
 
Q

Qu0ll

Dirk Bruere at NeoPax said:
Just "Accessing static field"
Code example:

public class MediaLists
{
public static DefaultListModel dvdListModel = new defaultListModel();
...

public void doStuffTo_dvdListModel(){}

}

MediaLists MedialList;
MediaList.doStuffTo_dvdListModel(); //Triangle and lightbulb with
//"Accessing static field" here

You haven't shown any code which actually accesses the static field so I am
assuming you are accessing it without prefixing it with MediaLists. which is
why it is warning you.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
K

Knute Johnson

Dirk said:
Just "Accessing static field"
Code example:

public class MediaLists
{
public static DefaultListModel dvdListModel = new defaultListModel();
...

public void doStuffTo_dvdListModel(){}

}

MediaLists MedialList;
MediaList.doStuffTo_dvdListModel(); //Triangle and lightbulb
with //"Accessing static field" here

I don't know NetBeans but try making it final.
 
Q

Qu0ll

Dirk Bruere at NeoPax said:

Is that shorthand for "that was the problem"?

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
(e-mail address removed)
[Replace the "SixFour" with numbers to email me]
 
N

Nigel Wade

Dirk said:
Just "Accessing static field"
Code example:

public class MediaLists
{
public static DefaultListModel dvdListModel = new defaultListModel();
...

public void doStuffTo_dvdListModel(){}

}

MediaLists MedialList;
MediaList.doStuffTo_dvdListModel(); //Triangle and
lightbulb with //"Accessing static field" here

This code does not compile and is far from complete.

When I attempt to make it complete and compilable I don't get any such warning.
Perhaps you need to supply some real code which exhibits the problem.

Also, it might help if you named your variables according to the normal rules,
lowercase first character for a variable, then there's less chance of you
confusing MediaList (the object) and MediaLists (the class). I say this
especially because when I use this code:
MediaList.dvdListModel.add(1, args);
I do get the warning.
 
N

Nigel Wade

[Hit send by mistake]

The reason that gives the warning is that you are accessing a static field via
an object reference. You might otherwise think that the field you are accessing
is member of the object rather than a shared reference to a static field of the
class.

You should really access the static field via the static class reference:
MediaLists.dvdListModel...
which may be what you meant to do, but you got confused between the object and
the class names because you didn't differentiate them by case.
 
D

Dirk Bruere at NeoPax

Nigel said:
This code does not compile and is far from complete.

When I attempt to make it complete and compilable I don't get any such warning.
Perhaps you need to supply some real code which exhibits the problem.

Also, it might help if you named your variables according to the normal rules,
lowercase first character for a variable, then there's less chance of you
confusing MediaList (the object) and MediaLists (the class). I say this
especially because when I use this code:
MediaList.dvdListModel.add(1, args);
I do get the warning.

I suppose I could have extracted a couple of large(ish) chunks of code
but I thought the above would be enough to illustrate. Seems I was wrong.

--
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
 
D

Dirk Bruere at NeoPax

Nigel said:
[Hit send by mistake]

The reason that gives the warning is that you are accessing a static field via
an object reference. You might otherwise think that the field you are accessing
is member of the object rather than a shared reference to a static field of the
class.

You should really access the static field via the static class reference:
MediaLists.dvdListModel...
which may be what you meant to do, but you got confused between the object and
the class names because you didn't differentiate them by case.
Actually, I assumed that the compiler would know that
MediaLists.dvdListModel == MediaList.dvdListModel

I was probably correct, but it seems it's not good practice for some reason.

--
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
 
L

Lew

Dirk said:
Actually, I assumed that the compiler would know that
MediaLists.dvdListModel == MediaList.dvdListModel

The compiler does know that. What it doesn't know is that you
violated the naming conventions.
I was probably correct, but it seems it's not good practice for some reason.

Referring to a 'static' member via an instance reference is not good
practice partly because it falsely identifies the member as instance-
level when it's actually class-level. It falsely documents the lookup
rules to resolve the reference, which differ between instance and
'static' members. It falsely requires there to be an instance
variable through which to reference the 'static' member, but does not
require that the reference is non-'null'. This can lead to
confusion. When code misrepresents itself to maintenance programmers,
such as through naming convention violations that are confusingly
close in the presence of instance references to 'static' members, that
leads to confusion, and confusion leads to error.
 
A

Andreas Leitgeb

Dirk Bruere at NeoPax said:
Actually, I assumed that the compiler would know that
MediaLists.dvdListModel == MediaList.dvdListModel
I was probably correct, but it seems it's not good practice for some reason.

Since it isn't javac, but netbeans that complained, its no big deal.

I've noticed javac to produce some less-than-optimal bytecode (it loads
the value of the reference onto the stack and then discards it, but
even that can be believed to be optimized away "just in time".

What remains is, that in actual code, when using static fields (or methods)
on such-typed references, someone looking at that code later may be tricked
into believing it was a non-static field/method and perhaps do something
goofy as a result of this misbelief. This someone could easily be yourself
half a year from now.
 

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

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top