Archived

This topic is now archived and is closed to further replies.

sab3156

Classes in Java

Recommended Posts

sab3156    109
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.

Share this post


Link to post
Share on other sites
darookie    1441
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]

Share this post


Link to post
Share on other sites
loserkid    122
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.....

Share this post


Link to post
Share on other sites
sab3156    109
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.

Share this post


Link to post
Share on other sites
loserkid    122
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

Share this post


Link to post
Share on other sites
sab3156    109
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]

Share this post


Link to post
Share on other sites
Martee    476
That''s because age and phone are private member variables of the class User. If you want them to be directly accessible from the outside world, you must make them public (which isn''t always a good idea ...). The alternative is to provide setAge() and setPhone() functions, so that instead of saying ''bob.age = 15'', you would say ''bob.setAge(15)''.

Share this post


Link to post
Share on other sites
PaleRaider    122
sab3156 you currently have age and phone set with "package" access, which is java''s default. Seeing as you didn''t declare either User or NewUser to be in a specific package NewUser will not be able to access those memebers. As was stated you could make age and phone "public" but this is undesirable. You could declare them to be protected and have NewUser extend User. Other alternative being making them private and providing accessor/settor methods as someone mentioned.

Share this post


Link to post
Share on other sites