• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B doubt related to Shadowing...

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At which line compiler complains?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why it is....


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

Can you please post the compile error you have ?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 37
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
chaitanya gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Raghavan,

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

thanks,
Chaitanya.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi debasmita,

Please refer this thread. It may help you understand the output.
 
reply
    Bookmark Topic Watch Topic
  • New Topic