my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes jls 15.10.1 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 "jls 15.10.1" Watch "jls 15.10.1" New topic
Author

jls 15.10.1

faiza haris
Ranch Hand

Joined: Oct 17, 2000
Posts: 173
Plz can someone explain this
the Primary expression is indeed fully evaluated at run time, despite the fact that only its type, not its value, is used to determine which field to access (because the field mountain is static).
Using type to access field???
what does this mean?
thanx in advance
ravi chan
Greenhorn

Joined: Nov 02, 2000
Posts: 11
If I understood your question correctly , type means whether your data type is a primitive or a referance type. Please correct me if iam wrong
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
this mean that call to static field does resolve at run time and it depend upon type of expression since it is static appropriate meber is called.
class Test{
static int mountain=10;
public static void main(String yogi[]){
Test x=null;
System.out.println(x.mountain);
}
Even if u use null there wiil not be exception
coz at runtime the expression is evaluted and since mountain is static it is called
irrespective of value of remaining expression.

if u make field mountains non static there will be NullPointerexception.
So its type of expression at runtime determine which field to called.
Hope this will help


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
faiza haris
Ranch Hand

Joined: Oct 17, 2000
Posts: 173
thanx... i got it...isnt jls hard? howcome u understood it well...hmmmm or maybe it takes time???
any tips?
faiza haris
Ranch Hand

Joined: Oct 17, 2000
Posts: 173
i was playing with yor code
and wanted to ask ...if it will also hold for a method in main with static variable???
class Testjranch{
static int mountain=10;
public static void main(String yogi[]){
void method(){
static int highpeak = 7;
}
Test x=null;
System.out.println(x.mountain);
System.out.println(x.highpeak());
}
The above is not compiling........
does the method have to be static or ?
ravi chan
Greenhorn

Joined: Nov 02, 2000
Posts: 11
Faiza,
Try x.method().highpeak in your print statement.
The reason you are getting an error is because the compiler does not know the variable highpeak, and you have to take out the static modifier within the method. You cannot use static within a method.
Sorry for my first reply, I had no clue what you were talkng about. Its always good to post the code.
ravi
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
sorry i paste it twice
[This message has been edited by yogesh sood (edited November 03, 2000).]
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
First of all i think u ant to write code like
class Testjranch{
static int mountain=10;
public static void main(String yogi[]){
void method()
{
static int highpeak = 7;
}
Testjranch x=null;
System.out.println(x.mountain);
//System.out.println(x.highpeak());
System.out.println(x.highpeak);
}
Don't confuse
Methods can only be declared in classe and in interface u cannot declare method in a method>
See u can find that ur self
when u will run above code it will lots of error and first error will be(Using Jdk1.2)
Testjranch.java:4: '}' expected.
coz when compiler parsing ur code it found main method and know compiler look for its Opening brace and now look for closing brace
........ but wait what is this it got other method declaration without geting closing brace so Mr. smarty compiler assume that u want to make this method as member of class but by mistake u have forget to close brace of first method this is way it said at line 4 that provide closing brace of main method..
this is not case with main method u will get same error with any other class or instance method coz u can't declare method in a method.
now come to ur second mistake .
if a varible is static it is class varible it will be intialized at loading of class and u can't have static varibles in method if u will try to compile a code with static varible in a method compiler will give u same error for the same reason as above.

it will look for closing brace of method and it got a static varible in method so it will give u error.

Now there is another major snag in this code
Now u have written x.highpeak By the way what is scope of highpeak only that method in which varible is defined it is local to method . x which is reference varible of Testjranch has nothin to do with highpeak.
Hope this will help u there is no shortcut to hardwork studying JLS really takes alot from u but also give alot to u .
try to study Jls carefully line by line i started from chapter 4 read the line once,twice look for example for that topic in Jls if example is not given in that section look for example in other section related to that topic.....,try to write code what u r understanding from that lines and at last Post it on Javaranch as u did.
don't forget to paste important point in other file from Jls so next time when u read Jls read ur that file instead of jls and if u have any doubt jump to that section and have look at that.
some topic in Jls are out of world for me that are not in objective i tend to skip them.
imean to say some chapters are easy in Jls and very important like Chapter5, 6,8 ,14,15 and so on im still on 14
it does,n mean that other chapter can be ignored but these chapters have points not given in most books.

Bye
[This message has been edited by yogesh sood (edited November 03, 2000).]
 
 
subject: jls 15.10.1