File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes something puzzled about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "something puzzled about "static"" Watch "something puzzled about "static"" New topic
Author

something puzzled about "static"

zhu weitao
Greenhorn

Joined: Sep 02, 2008
Posts: 9
code about:


the var "maxElements" in class "Use" is declared "static",
but here ,it's used in this method "this.maxElements",
does it belong to a class not a object?

I tried it,and it did compile and run correctly

why???
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

this.maxElements = maxElements;


At the time of compilation compiler will treat this statement as
Use.maxElement.
Static variables are always class variables.
Ben Smither
Greenhorn

Joined: Sep 09, 2008
Posts: 4
Java allows to use a reference to an object instance to refer static variables and methods. The this in the constructor is the same as an object reference "useA" in the example as follows.
Use useA = new Use();
useA.maxElements = 100;



Originally posted by zhu weitao:
code about:


the var "maxElements" in class "Use" is declared "static",
but here ,it's used in this method "this.maxElements",
does it belong to a class not a object?

I tried it,and it did compile and run correctly

why???
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
You just cannot use "this" or "super" within Static methods.
It will give a compiler error.
[ September 11, 2008: Message edited by: Nabila Mohammad ]

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

Java allows to use a reference to an object instance to refer static variables and methods. The this in the constructor is the same as an object reference "useA" in the example as follows.
Use useA = new Use();
useA.maxElements = 100;

java allow us to use objects to refer to static variables and methods.
But we should not use objects references to refer to statics.....
At compilation time object references get repleces with class name.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: something puzzled about "static"