Reputation: 179
I am wondering how to use a class from another package
here's a file that inside the package project1
package project1;
class student{
void print(){
System.out.println("s");
}
}
class teacher{
void print(){
System.out.println("t");
}
}
public class Project1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
here's a file inside a the package project2
package project2;
import project1.student;
public class Project2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
student x = new student();
}
}
but I am getting a error that student is not in public,
Am I importing the student class in a wrong way that I can't use the student class , or it's impossible to do this?
Upvotes: 1
Views: 14357
Reputation: 26961
Check this link
If a class doesn't have a modifier (public
, private
...) it uses the default modifier, that means the class can only be accessed from the same package.
In this this table you will see easily:
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N // this is your class
private Y N N N
So you need to declare Student
class as public in a separated file, or put Project2
in the same package than Student
and Teacher
public class student{
void print(){
System.out.println("s");
}
}
Upvotes: 4
Reputation:
Problem is your class student has default modifier which restrict the access level upto same package(not accessible outside the package project1
to package project2
). You can have only one public class
in one java file
.
Try this:
student.java
in the package project1
public class student
in the said
fileUpvotes: 1
Reputation: 7042
You are having trouble with access modifiers.
Your class student
is now package private
(no modifier). Make it public
to use outside the package
public class student{
}
Read accesscontrol For detail.
Upvotes: 0
Reputation: 2044
you should declare your Student
class as public, as follows:
public class student{
void print(){
System.out.println("s");
}
}
Anyway, having multiple classes, which are not inner classes, on the same file is a bad practice as per OOP development methodologies. It is also bad for maintenance, debugging, reusable components and modularity.
I suggest that you will also split the classes declaration into separate file (even if classes are small and simple)
Upvotes: 0
Reputation: 71
First of all, you can't have two main methods.
Try making the classes static so you can access them with Class.etc;
Or you could try creating a new object of each class (I think this is better). Remove the second main method, only leave one. And make this the first one:
public static void main(String[] args){
Student student1 = new Student();
Now you can access the student class using this object with student1.Something. Do the same for all classes except for the one containing the main method. }
Also make the classes start with a capital letter.
Upvotes: 1