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).]
Matt DeLacey
Ranch Hand
Joined: Oct 12, 2000
Posts: 318
posted
0
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.
Rajiv Ranjan
Ranch Hand
Joined: Sep 28, 2000
Posts: 61
posted
0
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. -----------------------------------------------------------------
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.
Shubhangi A. Patkar
Ranch Hand
Joined: Sep 20, 2000
Posts: 78
posted
0
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.
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
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
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Prasad Ballari
Ranch Hand
Joined: Sep 23, 2000
Posts: 149
posted
0
Ajith, Thank you for your valuable advice. Regards Prasad ------------------