• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic