Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using class of the package in other classes in the package?

 
Sloan Bowman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use objects from classes with the same package you are creating? I keep getting errors. Below is an example
package com.test;
import com.test.*;
public class Test
{
public Test()
{}
public static void main(String[] args)
{
Users u = new Users();
u.setUsername("SloanB");
String name = u.getUsername();
System.out.println(name);
}
}

Second class
package com.test;
public class Users
{
private String username;
private String password;
public Users()
{
}
public void setUsername(String name)
{
this.username = name;
}
public String getUsername()
{
return(this.username);
}
} // end class

Thanks for the help as usual
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q)Is it possible to use objects from classes with the same package you are creating?
A)Yes it is possible.
There is no need of import class of same package (It is just a optional).
The problem what I guess you are facing is incorrect way of calling the cass.
Follow this step to make your code work.
1)set the classpath. i.e, set classpath=%classpath%;c:\example;
Here replace c:\example with the folder name where your com folder is located.
2)Then execute the main method with the fully qualified name. That is
Java com.test.Test

-arun
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic