Results 1 to 11 of 11
  1. #1
    Erre is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Getters and setters

    Greetings,
    Im studying java at school and they told us in labaratory to learn from their documentation and to solve some problems.
    Well, this is what im doing!!!
    The problem says to make every field of class private and to encapsulate the fields using getters methods accesable for all classes.
    I made for every field
    public String getField1() {
    return Field1;
    }
    But it says me the constructor is undefined.
    Well, what i want to ask you is : Do i have to use also setters method??? When they said : Getters methods they meant getters and setters or there is a way of not using setters???

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Getters and setters

    You're going to have to provide more information than that. Where's your SSCCE? What is the exact text of the error? What line (in the SSCCE) does it occur on?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Erre is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Getters and setters

    Sorry for lack of information.
    I have 2 classes, mainclass and a public class.
    In the public class i made a constructor which initialize all fields of its. The filerds type is string.
    In public class i also made a print() method.
    Well, before starting the thread i used to "manual" create and intialize the fields like:
    Classname object_1=new Classname();
    objhect_1.field1= "somthing";
    Meanwhile i deleted this and i used the constructor and it seems the problem its fixed.

    What i dont understand now:
    1.If i delete getters nothing changes. Can you explain me why???
    2.I can initialize the fields only using the constructor or setters - Do I can initialize the private fields using the constructor because the constructor belongs to the class where are the fields???? I thought any private field can be changed only using setters.

    Here is my code:

    Java Code:
    public class Message {
    	private String sender="test if it changesl";
    	private String receiver;
    	private String content;
    	public void print(){
    		System.out.println("From <"+sender+"> to <"+receiver+">: <"+content+">");
    	}
    	Message(String _sender, String _receiver, String _content){
    		sender=_sender;
    		receiver= _receiver;
    		content= _content;
    	}
    	 Message(String _sender,String _content){
    		 sender= _sender;
    		 content= _content;
    		 
    	 }
    	 public String getContent() {
    		return content;
    	}
    	 public String getReceiver() {
    		return receiver;
    	}
    	 public String getSender() {
    		return sender;
    	}
    
    	 public void setContent(String _content) {
    	       this.content = _content;
    	    }
    }
    Java Code:
    class TestClass {
    	public static void main(String[]args){
    		Message mesaj_1=new Message("1","2","3");
    		mesaj_1.setContent("test the setter");
    		Message mesaj_2=new Message("Andre","John");
    		Message mesaj_3=new Message("Andre","John");
    
    		mesaj_1.print();
    		mesaj_2.print();
    		mesaj_3.print();
    	}
    
    }
    Last edited by Erre; 10-25-2012 at 05:07 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Getters and setters

    Sorry, but what is your actual error? Please post the full text of the actual error you're asking about- you're asking about 5 separate questions at one time, and I'm a little confused by which one is the actual problem.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Erre is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Getters and setters

    There are no errors.
    I said. The error was fixed after i used the constructor. Before, i used to manual initialize the fields like.
    object1.field1="somthing".
    I just want to understand how the program works.
    I cant figure out what`s the role of getters, if i delete them nothing change, and why i can initialize private fields using a constructor in testclass.
    Last edited by Erre; 10-25-2012 at 08:07 PM.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Getters and setters

    Quote Originally Posted by Erre View Post
    There are no errors.
    I just want to understand how the program works.
    Then I would recommend stepping through this with a debugger. If you're confused about something in particular, I recommend asking one specific question at a time.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Erre is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Getters and setters

    Well.
    First question:
    Im extremly confused about this.
    The only usefull thing about getters that ive found is
    Java Code:
    System.out.println(mesaj_2.getContent());
    So why i must use getters when it works the same with print()????? - This is the first,last and the only question from my post.
    Last edited by Erre; 10-25-2012 at 09:03 PM.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Getters and setters

    I'm still not really sure what you're asking, but let me take a stab at it:

    Calling the print() function prints out a few things, including the content.

    But maybe you only want to print out the content, without any of that extra stuff.

    So instead of calling the print() function, you call the getContent() function and do with it whatever you want.

    The two functions do different things, which is why you need them both. Is that your question?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Erre is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Getters and setters

    Hey bro, i understood.
    If i use in TestClass [see below code] , it doesnt work because the field isnt visible, so i must use the print() method from Message class or the getters method.
    Java Code:
    System.out.println(object.field)
    Is this correct??? this is the only way i can use getters or setters??? - 2 questions, i know :P.

  10. #10
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Getters and setters

    If you're just looking for a brief explanation, here goes:


    ANY method in the "private" class Message can access/change any private variables that are available to that class. The getters are for the instantiating class to be able to view those variables. The setters are for the instantiating class to be able to set those variables.

    private variables protect the class from outside manipulation, but not from the class itself changing things.

  11. #11
    Erre is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Getters and setters

    Yes.
    This is what what i was looking about.
    I thought you can acces the fields only using setters or getters, but it works with any public method from Message class. Also, getters is just another method to view the fields, same as print() method and setters are similar to My constroctor method. The difference is that setters and getters acces/instantiate only one field.
    Thanks for help,
    Good luck!

Similar Threads

  1. Confused about getters and setters
    By jnjh in forum New To Java
    Replies: 8
    Last Post: 05-03-2011, 08:12 AM
  2. Do getters & setters effect the performance?
    By malaguena in forum New To Java
    Replies: 6
    Last Post: 03-12-2011, 08:46 PM
  3. Positions and values, getters, setters
    By Malus in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 06:55 PM
  4. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 02:47 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 11:57 PM

Posting Permissions

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