0

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{
...
}
4
  • It says it says the classes Three and Four should be public. Commented Dec 13, 2016 at 21:38
  • You said Five is a project. Is it a class? If so, Three and Four are inner classes? Make them public as well (and if you have no Five instance, make them static classes). Commented Dec 13, 2016 at 21:48
  • It would be much easier to try and assist you if you provided a Minimal, Complete, and Verifiable example stackoverflow.com/help/mcve Commented Dec 13, 2016 at 21:48
  • @GeneMarin Yes Five is a new project containing class Five(empty). I cannot paste the other two classes inside this class. I have to paste them outside, and access them in my main project. Commented Dec 13, 2016 at 21:51

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I cannot put classes Three and Four in separate files(Part of the question). So basically i cannot make them public because class Five is public. i also cannot put them inside one public class. So now the only solution is to make this file a part of the main package? I'll try it out later.
0

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.