Results 1 to 5 of 5
- 02-09-2011, 11:39 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
Objects & Classes - Beginner question
Ive just started a programming course but i have entered a semester behind so im rushing to catch up. My question is why is this not printing the correct name, phone and id????
Class file
Test fileJava Code:public class Student { private String name; private String phone; private String studNo; public Student(String name, String phone, String studNo){ this.name = name; this.phone = phone; this.studNo = studNo; } public String getName(){ return this.name; } public String getPhone(){ return this.phone; } public String getStudNo(){ return this.studNo; } }
The out put is -Student@3ae48e1bJava Code:public class StudentTest { public static void main(String[] args){ Student s1, s2; s1 = new Student("Jony", "042", "123"); s2 = new Student("Michael", "123465", "98765"); System.out.println(s1); System.out.println(s2); } }
Student@732dacd1
also if i change the phone and studNo to int it gives me this error msg - "The constructor Student(String, String, String) is undefined"Last edited by Eranga; 02-09-2011 at 12:00 PM. Reason: code tags added
- 02-09-2011, 12:04 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
s1, s2 are objects, and you just print them.
Think like this, object is just a blue-print of your class with all the information you have. So you have meaning of it with the individual properties.
- 02-09-2011, 12:07 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
To retrieve the information you've to call the public accessors. For example, if you want to display the name of the student s1,
Do you about what the dot operator does?Java Code:System.out.println(s1.getName);
- 02-09-2011, 12:42 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 38
- Rep Power
- 0
Ahhhh. Thakyou..
I altered it so it read:
System.out.println("Name: " + s1.name + " Phone: " + s1.phone + " Student number: " + s1.studNo);
System.out.println("Name: " + s2.name + " Phone: " + s2.phone + " Student number: " + s2.studNo);
Thanks Eranga.
- 02-09-2011, 12:49 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are welcome. :)
If you satisfied with the answer, please mark the thread solved.
Similar Threads
-
Beginner - question of 'if' statement
By hayden06f4i in forum New To JavaReplies: 6Last Post: 11-08-2010, 02:45 AM -
really quick question for beginner
By its_crawford in forum NetBeansReplies: 4Last Post: 06-17-2010, 04:14 AM -
Beginner question about ArrayList
By kesi in forum New To JavaReplies: 3Last Post: 09-19-2009, 11:30 PM -
Another beginner question for AP test
By DanK in forum New To JavaReplies: 1Last Post: 04-27-2009, 05:36 AM -
Beginner Java question
By DanK in forum New To JavaReplies: 3Last Post: 04-27-2009, 04:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks