1

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?

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
user47103
  • 179
  • 1
  • 6
  • 17
  • 1
    Use `public class student {...` and move the `teacher` and the `Project1` into separate files - there can be only one public class in each file. – vojta Jul 08 '15 at 08:46
  • 1
    The class should be public to access it outside the package. `public class student` – SASIKUMAR SENTHILNATHAN Jul 08 '15 at 08:46
  • 2
    You should just use "public" in front of your Student class. That is exactly what the error is saying – LBes Jul 08 '15 at 08:47
  • Cmon @vojta, he already has a public class in the classfile containing staudent class ...public class Project1 . That wouldnt be possible. You would need to define the student class in a separate class file and make it public, or either create a class with default access and place it in package 2 . – Kalyan Chavali Jul 08 '15 at 08:49
  • 1
    See http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private – Reporter Jul 08 '15 at 08:50
  • Also, use Java naming conventions. Eclipse already complained at you for not capitalizing them. – chrylis -cautiouslyoptimistic- Jul 08 '15 at 08:53
  • Thx guys, It works now :) – user47103 Jul 08 '15 at 10:10

5 Answers5

4

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:

Access Levels

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");
    }   
}
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

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:

  1. Create new file student.java in the package project1
  2. Create public class student in the said file
1

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.

BlerpyDude
  • 71
  • 5
0

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)

Ziv Levy
  • 1,904
  • 2
  • 21
  • 32
0

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.

Saif
  • 6,804
  • 8
  • 40
  • 61