aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange 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 "Strange" Watch "Strange" New topic
Author

Strange

amit mawkin
Ranch Hand

Joined: Oct 31, 2001
Posts: 73
please consider this code
<code>

public class Test006 extends Object
{
static int staticTotal = 10;
int nonStaticTotal = 20;
static void method1()
{
staticTotal = 30; //1
Test006.staticTotal = 30; //2
new Test006().staticTotal = 30; //3
}
void method2()
{
staticTotal = 30; //4
this.staticTotal = 30; //5
Test006.staticTotal = 30; //6
new Test006().staticTotal = 30; //7
}
}

</code>
The strange part that I feel is that this code compiles.
in method2() please look at line marked 4 clearly showing a non static metod trying to call a static value.
The mock from where I picked this questions asks which marked lines will not compile.
In ans it was only stated nothing is wrong with the code
which is indeed a fact.
can anyone clear my doubts
------------------
coffee drinker
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]
[This message has been edited by amit mawkin (edited December 07, 2001).]


regards, Amit
coffee drinker and Sun Certified Programmer for Java 2 Platform
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Yep the answer is correct. There is nothing wrong in accessing a static member from a non-static method. What is wrong is trying to do the opposite: accessing non-static member from static methods. An instance can access all information about the class (static info) it wants to. But the class by itself cannot access instance info (non-static info). You have to really make the difference between a class and an instance of a class. Once you get the concept, you'll see those question won't be a probleme anymore.
Here is an informal example. Consider the class Cat. Also consider two Cat instances, Bob and John (weird name for cats ). Now the class Cat knows nothing about (non-static) members of Bob and John. That means that a class (static) cannot access non-static members.
Bob and John, in the contrary, know that a Cat has four legs (nbLegs is a static member of class Cat). That means that an instance (non-static) can access static info (class info).
That's maybe not the best example but the only that came up so early on a Friday morning .
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 07, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
amit mawkin
Ranch Hand

Joined: Oct 31, 2001
Posts: 73
Thanks Val
gosh i dint knew that.
------------------
coffee drinker
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
JLS 8.4.3.2

A method that is declared static is called a class method. A class method is always invoked without reference to a particular object. An attempt to reference the current object using
the keyword this or the keyword super in the body of a class method results in a compile-time error. It is a compile-time error for a static method to be declared abstract.
A method that is not declared static is called an instance method, and sometimes called a non-static method. An instance method is always invoked with respect to an object,
which becomes the current object to which the keywords this and super refer during execution of the method body.

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange