Results 1 to 5 of 5
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default 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
    Java 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;
    		
    	}
    	
    	
    }
    Test file
    Java 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);
    		
    		
    	}
    }
    The out put is -Student@3ae48e1b
    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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    To retrieve the information you've to call the public accessors. For example, if you want to display the name of the student s1,

    Java Code:
    System.out.println(s1.getName);
    Do you about what the dot operator does?

  4. #4
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Beginner - question of 'if' statement
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-08-2010, 02:45 AM
  2. really quick question for beginner
    By its_crawford in forum NetBeans
    Replies: 4
    Last Post: 06-17-2010, 04:14 AM
  3. Beginner question about ArrayList
    By kesi in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 11:30 PM
  4. Another beginner question for AP test
    By DanK in forum New To Java
    Replies: 1
    Last Post: 04-27-2009, 05:36 AM
  5. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 04:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •