• 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

Tricky Concept of JVM.

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This i found out recently.I would like to share with all respected javaranch people.
Q)Which is the default look up for class object during class loader until othereise specified. i.e assume that you have created two classes out of which one is called 'StringBuffer' and another one is called 'Test' which uses the 'StringBuffer' class.(both are stored in the same default package).
As we know that 'StringBuffer' is already availble in java.lang package.Now the queston, during compilation(or class loader)which class the JVM picks up for further processing until otherwise specified..
1) your default 'StringBuffer' class
2) java.lang.StringBuffer class
Regards
Prasad

------------------

[This message has been edited by Prasad Ballari (edited November 15, 2000).]
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...this is interesting. My test revealed that it uses the StringBuffer class that I defined, but I'm not sure why.
import java.lang.*;
class StringBuffer
{
int x = 3;
int getX()
{
return x;
}
}
public class B
{
public static void main(String args[])
{
StringBuffer s = new StringBuffer();
System.out.println(s.getX());
}
}
prints out 3...i would have thought there would have been a compiler error.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did a little modification to the code and now I get error as
" Method getX() not found in java.lang.StringBuffer"
So my guess is JVM looks first in default package unless otherwise specified.
-----------------------------------------------------------------

class StringBuffer
{
int x = 3;
int getX()
{
return x;
}
}
public class B
{
public static void main(String args[])
{
java.lang.StringBuffer s = new java.lang.StringBuffer();
System.out.println(s.getX());
}
}
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever there is a conflict between two classes bearing the same name :
1. the class in the local package gets preference.
2. the other class has to be addressed completely. (with its package name)
so, if u do not specify any package, java uses the local class (which is in its scope)
raj koura.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..

I think that the problem is more related to the classpath defined by you, if you have defined any. Could you judt cross check it?
Because AFAIK, the classes are searched in the packages in the order in which they have been specified, so if you have put . as a classpath before you JDK path, this might happen.
PLease correct me if i am wrong...
Shubhangi.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class names are resolved starting from the immediate name space. This means if you define a class with the same name as the one exists in an outer scope, the class in the inner scope "hides" the class defined in the outer scope. Unless you explicitly refer to the class in the outer scope using full name qualifier ( eg., java.lang.StringBuffer ) JVM always uses the class in the immediate (inner ) scope.
For more information you may read JLS Section 6.3 Scope of a Declaration
Hope that helps,
Ajith
 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith,
Thank you for your valuable advice.
Regards
Prasad
------------------
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic