简体   繁体   中英

precedence of class of local .java file or imported class from package in java?

one class using another class which is parallel to it(ie .java file existing in same folder as in that class) and it is also getting the same class from another package by importing to that class from another folder, priority should be given local class ie .java existing in same folder but it is running from pacakage from another folder ? code is mentioned below:

\\\\ .java file in same folder\\\\

public class Temp1
{
public void show()
    {
    System.out.println("show from local");
    }
public static void main()
    {
    Temp1 t1=new Temp1();
    t1.show();
    }
}

\\\\.java file having same class in diff. folder \\\\

public class Temp1
{
public void show()
    {
    System.out.println("show from global");
    }
public static void main()
    {
    Temp1 t1=new Temp1();
    t1.show();
    }
}

//here is the main class that i am running//

 package p2;
 import p1.*;
 class Temp2
 {
    public static void main(String... s)
    {
    Temp1 t1=new Temp1();
    t1.show();
    }
  } 

Help me out ...

Suppose your Temp1 and the mainClass are in package2 and you compile the main class then obviously the Temp1 of the same package will be compile and called.

but if Temp1 is in package1 and the other Temp1 in in package3 then in that case that specific class will be called whomever you add it to the classpath while compiling the mainClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM