Skip to main content
gamedev.net
🔒 Locked

Classes in Java

Started by sab3156 10 replies 1.7k views
Original Post
sab3156
sab3156
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.
darookie
darookie
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]
loserkid
loserkid
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
darookie
darookie
hey - loserkid seems to have posted almost at the same time
sab3156
sab3156
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.
loserkid
loserkid
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
sab3156
sab3156
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.
sab3156
sab3156
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.
Martee
Martee
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)''.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
PaleRaider
PaleRaider
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.