简体   繁体   中英

Accessing multiple classes from another java file

I am stuck on an assignment question where the task is only to break-down a single java file containing multiple classes into multiple java files and then import these java files so that the original project still works.(4 classes in total, moving 3 of them to separate files)

I create a new Project and move one class to that new project. Then import it to my original file and set the necessary functions to public and it works.

For the other two classes, I have to make a new Project with a completely different name(say Five ) and paste the clases Three and Four into this Project. And then import these two classes into the original file.

I do that and it says the classes Three and Four should be public. That however is not possible since the class Five is already public. How do I access these two classes from the original file?

Project One: (This is the one im trying to run)

package one;
import two.Two;
import five.Five;

public class One {
public static void main(String[] args) {
...
}
)

class Customer{
...//this class accesses attributes and methods of classes 
//Two, Three and Four. The error occurs for methods from classes Three and Four
}

Project Two

package two;
public class Two {
public static void main(String[] args) {
...
}
)

Project Five

package five;
public class Five {
public static void main(String[] args) {
...
}
}

class Three{
...
}

class Four{
...
}

Take a look at this question (and answer): Can a java file have more than one class?

You can only have one public class per file . If you don't want Three and Four as inner static classes in Five , you must put them in separate files Three.java and Four.java .

Also, package can be equivalent to a folder, so if your classes are in the same folder (part of one module/logic unit) they can all be in the same package, say main .

Thus your package main will contain all classes in a Java file each (it's also the good practice, unless a class is logically a sub-unit of another class). Also note that no imports are required in classes in the same package. They don't even have to be public.

as in above answer in one .java file has only one public class we cant make other class public so, other classes cannot be access by the classes of different packages because those classes are default and their visibility only in it's package. other package cannot access those classes.

But if you really want to do below example is one of the way to do that.

package pack1;
public class A {
    public void sum(int a, int b){
        System.out.println("Addition of a and b ="+(a+b));}
    public static class Sub
    {
        public void subtraction(int a, int b)
        {
            System.out.println("subtraction a-b ="+(a-b));

        }
    }     

}

// below main class is written and in that main class we are accessing the above classes of pack1

package mypack;
import pack1.*;

public class B {
    public static void main(String[] args) {
        A o1=new A();
        int a=50,b=20;
        o1.sum(a,b);
        A.Sub o2=new A.Sub();
        o2.subtraction(a,b);
    }
}

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