How to use a class from another package in java? -
i wondering how use class package
here's file inside 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 command line arguments */ public static void main(string[] args) { // todo code application logic here } }
here's file inside package project2
package project2; import project1.student; public class project2 { /** * @param args command line arguments */ public static void main(string[] args) { student x = new student(); } }
but getting error student not in public,
am importing student class in wrong way can't use student class , or it's impossible this?
first of all, can't have 2 main methods.
try making classes static can access them class.etc;
or try creating new object of each class (i think better). remove second main method, leave one. , make first one:
public static void main(string[] args){ student student1 = new student();
now can access student class using object student1.something. same classes except 1 containing main method. }
also make classes start capital letter.
Comments
Post a Comment