This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B doubt related to Shadowing... 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 "K&B doubt related to Shadowing..." Watch "K&B doubt related to Shadowing..." New topic
Author

K&B doubt related to Shadowing...

chaitanya gopal
Greenhorn

Joined: May 22, 2007
Posts: 13
class Bar
{
static int num=9;
}

class Foo
{
Bar b=new Bar();
void ChangeIt(Bar b)
{
b.num=23;
System.out.println("b.num in a method :"+b.num);
b=new Bar();
b.num=45;
System.out.println("b.num in a method now :"+b.num);
}
public static void main(String args[])
{
Foo f=new Foo();
System.out.println("f.b.num in main is:"+f.b.num);
f.ChangeIt(f.b);
System.out.println("f.b.num in main after ChangeIt is:"+f.b.num);
}
}

according to K&B it compiles fine and gives the output like below:
f.b.num in main is: 9
b.num in a method : 23
b.num in a method now : 45
f.b.num in main after ChangeIt is:23

but when i try to compile , it slaps me....
I am using jdk1.5 version.....

why it is....

thanks,
chaitanya.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
At which line compiler complains?


cmbhatt
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

why it is....

I'd like to ask the same It should run fine.

Can you please post the compile error you have ?


[My Blog]
All roads lead to JavaRanch
Pandharinath Barve
Greenhorn

Joined: Aug 04, 2006
Posts: 13
its compiling on 1.5 and giving o/p like this.

f.b.num in main is:9
b.num in a method :23
b.num in a method now :45
f.b.num in main after ChangeIt is:45


With Best Regards,<br />Pandharinath.<br />SCJP 5.0
Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37

The output is :

f.b.num in main is:9
b.num in a method :23
b.num in a method now :45
f.b.num in main after ChangeIt is:45


I use jdk6.0 and can compile correctly!


Head first Servlet and JSP
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Can you tell us what page in what K&B book (1.4 or 5.0)? The answer in the original post is wrong. What printing of the book do you have?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Chaitanya,

As others said, i dont think there would be an error. Can you please post the compiler error so that it would be helpful to look into it?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
chaitanya gopal
Greenhorn

Joined: May 22, 2007
Posts: 13
sorry friends,

its not a compile time error , its a runtime error as below:

D:\cnya\UnderBookRef>java Foo
Exception in thread "main" java.lang.UnsupportedClassVersionError: Foo (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)


thanks,
chaitanya.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

oh... this is the famous error which comes when there is a mismatch in the JDK versions.

The version you used while compiling was different than that of the one you use while running. Thats why this error!

Just check the same and try re-running it.

HtH.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I think you must have used JDK 1.4 to compile and JDK 1.5 to run.

the version number 49 is for the same.

Please correct those and run it again.
chaitanya gopal
Greenhorn

Joined: May 22, 2007
Posts: 13
Thank you Raghavan,

you are correct, after setting my path it works fine...

thanks,
Chaitanya.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689


I think you must have used JDK 1.4 to compile and JDK 1.5 to run.



you are correct, after setting my path it works fine...


So, .java code compiled under 1.4 doesn't automatically work under 1.5? We're talking about class files running from the IDE, right? What about deployed code? Can you specify the JRE version in the manifest in a Jar file?

For deliverables, I have the following code embedded in my application:


If you want to allow more than one version of the JRE to run your software, you may want to use the method split() to easily parse the version number instead of the method beginsWith().

Also, you won't find VAlert since it's one of my classes, you'd use JOptionPane instead.

Kaydell
[ June 01, 2007: Message edited by: Kaydell Leavitt ]
Amit Wadhwaa
Ranch Hand

Joined: Feb 15, 2007
Posts: 74
The problem occurs when you try to run a program compiled using a higher version(say 5.0) using a older JVM(say 1.4) and not in the reverse case. That is, a program compiled using Java 1.4 should run while using Java 5.


SCJP 5 94%<br /><a href="http://amit-wadhwa.blogspot.com/" target="_blank" rel="nofollow">My Blog</a>
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Yes, thats right.

A .java file compiled in a higher version may not run in a lower version. Thats why you get the error.

the number 49 is being put by the compiler as a result of its compilation when its being checked by the lower version of JRE it gives an error.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

So, the way that my code in my previous post tries to give an understandable error message won't work will it? I compile in Java 5.0 and if run in Java 4 or earlier, I'll get an exception before my code gets a chance to exit() more gracefully, won't I?

Kaydell
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
Hi everyone,
The code orginally posted by chaitanya was:

i am not unable to understand the output to this.
can anyone please explain me the same.
Thanks in advance


debasmita
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi debasmita,

Please refer this thread. It may help you understand the output.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B doubt related to Shadowing...