| Author |
referenc static variables with this in methods
|
jason stark
Greenhorn
Joined: Dec 01, 2002
Posts: 10
|
|
I'm preparing for the Sun Certified java exam. I've tried refencing a static variable from within a method using "this" and it worked. I didn't expect it to as the variables not attached to an object. Also I've managed to compile transient variables with the prefixed by the word static which I read was not possible. Also some times I've seen this as legal:- public static int main(String[] args) will this be legal for the exam thanks for any help Jason
|
 |
Svend Rost
Ranch Hand
Joined: Oct 23, 2002
Posts: 904
|
|
hi Jason Conserning the 2nd question about public static int main(String[] args) {}: First of all.. in the exam you have to use what the JLS says - not what the compiler states. And acording to the JLS the "main method" (the one that's called with "java ClassName") has to be: public static void main(String[] args) (as a side note, it can also be final) btw. It's perfectly okay to make a class with the above public static int main(String[] args) signature. Eg. will compile with no errors - but if you try to execute the file you'll get a java.lang.NoSuchMethodError: main Exception (cause it doesn't have a main method with the correct signature). /Svend Rost
|
 |
david eberhardt
Ranch Hand
Joined: Jul 02, 2002
Posts: 158
|
|
I agree with Svend, also If you put both methods in a file, public static void main (String [] args) {} public static int main (String [] args) {} you'll get a compiler error: You cannot overload a method by changing the return type ONLY.
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
|
Regarding your first question, nothing prevents you from referencing a static variable with "this", that is, prefixing the variable id with this. The type of the object referenced by "this" (i.e. an object of the class in which "this" is used or any subclass) will be used to resolve the static variable. Nothing is wrong about it, although the practice is not that good. You should always reference a static variable with the type the variable is declared in.
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Alfred Kemety
Ranch Hand
Joined: Aug 14, 2002
Posts: 279
|
|
To add, what's not allowed is to try to use the this keyword in a static method since static methods are not passed the this object reference... right?? [ December 02, 2002: Message edited by: Alfred Kemety ]
|
Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
|
 |
david eberhardt
Ranch Hand
Joined: Jul 02, 2002
Posts: 158
|
|
[Code reformatted by Val] [ December 03, 2002: Message edited by: Valentin Crettaz ]
|
 |
david eberhardt
Ranch Hand
Joined: Jul 02, 2002
Posts: 158
|
|
within an instance method, called with david.instanceMethod1() for instance, I can access other objects of the same class, say the terri instance by the following:
|
 |
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
|
|
The type of the object referenced by "this" (i.e. an object of the class in which "this" is used or any subclass) will be used to resolve the static variable.
A minor slip: variables are not resolved at runtime, the compile type of the variable used in the access expression is searched for declared or inherited fields, but not the type of the object pointed to by the variable. This means this code prints 0 because the compile type of "this" is Test:
|
SCJP2. Please Indent your code using UBB Code
|
 |
david eberhardt
Ranch Hand
Joined: Jul 02, 2002
Posts: 158
|
|
Originally posted by Jose Botella: This means this code prints 0 because the compile type of "this" is Test:
Jose: Wow - if this had been a test question, I would have got it wrong! I think what threw me off is that I am not used to seeing the same variables that are declared in the superclass, redeclared in the subclass as is in the case above. So I am not sure what behaviour this would cause. Anyways, since I plan on inheriting things in the subclass, I rewrote the code to the following: Your code and information was a real eye opener though!
|
 |
jason stark
Greenhorn
Joined: Dec 01, 2002
Posts: 10
|
|
Originally posted by david eberhardt: <hr></blockquote> Jose: Wow - if this had been a test question, I would have got it wrong! I think what threw me off is that I am not used to seeing the same variables that are declared in the superclass, redeclared in the subclass as is in the case above. So I am not sure what behaviour this would cause. Anyways, since I plan on inheriting things in the subclass, I rewrote the code to the following: Your code and information was a real eye opener though![/QB]
Hi I tried this bit of code and removed the this bit removed and it made no diffence to what was printed. Thanks to everyone who replied Jason
|
 |
 |
|
|
subject: referenc static variables with this in methods
|
|
|