Classes in Java

Started by
10 comments, last by sab3156 21 years, 3 months ago
i''m probably shaming myself by asking this easy java question...but hey, im a newb what''s wrong with this source?
  
public class User {
  // variables

  int age;
  long phone;

  // user information.

  void UserInfo(int age, long phone) {
    System.out.println(age);
    System.out.println(phone);
  }

}  // end of class.



class NewUser {

  // creating a new ''User'' object.

  User bob = new User();

  // initializing bob''s user info.

  bob.age=15;
  bob.phone=12341234;

  // outputting info.

  UserInfo(age, phone);

}  // end of class.

  
thanks in advance.
Air-Conditioners are like computers. They stop working when you open windows.
Advertisement
Whats the error your getting?
Make your methods "public".


  public class User {       // variables       int age;      long phone;       // use get/set methods or constructors     // to access/manipulate dta members!     public User(int _age, int _phone) {          age = _age;          phone = _phone;     }       // user information.     public void UserInfo() {            System.out.println(age);        System.out.println(phone);       }}  // end of class.// what's that intended to be for?public class NewUser {       // creating a new 'User' object.       User bob = new User(15, 12341234);  // initializing bob's user info.     bob.UserInfo(); // it's another class, so query bob's attributes}  // end of class.  


It seems you're not quite familiar with OO design and concepts.
I'd suggest you to read some tuts on OO design and concepts
first.

Good luck!
Pat

[edited by - darookie on January 29, 2003 9:05:56 PM]
hey,

for starters, you can''t have executable code ie method calls, assignment statments , .. etc that are not in methods/constructors (except var declaration ..). Futhermore, the method "UserInfo" cannot be called without an instance of the class "User" being created ie "bob", and to call the method "UserInfo" you must do: bob.UserInfo(..). So your class "NewUser" should look like:


  class NewUser {    User bob = new User();  // this is ok      public NewUser() {     bob.age=15;       bob.phone=12341234;       bob.UserInfo(age, phone);  }}    


now I assume you''ll want to execute the code in some fashion, so in your "NewUser" class (just as an example) declare this method:


  public static void main(String[] args) {  NewUser usr = new NewUser();}  


hope that helped (and made some sense).

oh and you may want to check out this tut

jp.

==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
From what i can see this is what u should do...

public class User
{
// variables
private int age;
private long phone;

//constructor
public User(int age, long phone)
{
this.age = age;
this.phone = phone;
}

public int getAge()
{
return age;
}

public long getPhone()
{
return phone;
}

// user information.
public void UserInfo()
{
System.out.println(age);
System.out.println(phone);
}
} // end of class.

class NewUser
{
// creating a new ''User'' object.
// initializing bob''s user info....now done in constructor
User bob = new User(15, 12341234);

// outputting info.
bob.UserInfo();

} // end of class.


I think this should now work.....
hey - loserkid seems to have posted almost at the same time
haha, darookie

OK, yeah, i guess i do need to learn more... i just started learning about OO in c++, and decided to check it out in Java...probably not the best idea in the world at this point. thanks for all your help, i learned a great deal.
Air-Conditioners are like computers. They stop working when you open windows.
hi;

Perhaps I didn''t make myself clear enough, but you cannot have ANY executable code that''s not in a method or a constructor. So the following code will not compile (try it and see):


  public class NewUser {                              User bob = new User(15, 12341234);  // this is ok   bob.UserInfo(); // cannot do this, needs to be within a                   // method/constructor}    


However, this WILL compile:


  class NewUser {     public static void main(String[] args) {      User bob = new User(15,12341234);      bob.UserInfo();   }   }  


So the code above will create a new instance of the class "User", initializes it with (15,12341234) and print out the data to the command line. Notice that I made class "NewUser" not public since I am assuming your writing your code in file called "User.java" and you cannot have two public classes in the same file. So only "User" can be public. If you would like both classes to be public create a new file, name it NewUser.java and copy and paste the code in above and make the class "public".

Just as a side note:if you are using "package"s in your code, the method "UserInfo" need not be neccessarily public since the classes are in the same file, and if you do not put a member access modifier (private,protected,public), the default is "package" which means that all classes in the same package can access the method "UserInfo" and the member variables.

BTW, why would you be shaming yourself by asking a java question? Shame in being a java question? or shame in being an "easy" question? Since neither are shameful - you''re just starting out.

anyway, I hoped that helped and made some sense too
jp.

==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
thank you jp. i understand, thanks woohoo! im off to learning java now. hehe.
Air-Conditioners are like computers. They stop working when you open windows.
lol ok wait...


i have this:

public class User {    // variables.  int age;  long phone;  // user info.  public void UserInfo(int age, long phone) {    System.out.println(age);    System.out.println(phone);  }}  // end of class.class NewUser {    User bob = new User();  public NewUser() {    bob.age=15;    bob.phone=12341234;    bob.UserInfo(age, phone);  }  public static void main(String args[]) {    // i don't know what you meant by...    // NewUser usr = new NewUser();  }}  // end of class.   



now, i have errors, due to unaccessible variables, age and phone. i can't access them from class NewUser?? what's the problem?

[edited by - sab3156 on January 31, 2003 12:08:45 AM]
Air-Conditioners are like computers. They stop working when you open windows.

This topic is closed to new replies.

Advertisement