Results 1 to 11 of 11
Thread: Getters and setters
- 10-25-2012, 03:15 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
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..gif)
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???
- 10-25-2012, 03:36 PM #2
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!
- 10-25-2012, 03:53 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
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 04:07 PM.
- 10-25-2012, 05:54 PM #4
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!
- 10-25-2012, 06:54 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
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 07:07 PM.
- 10-25-2012, 07:01 PM #6
Re: Getters and setters
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-25-2012, 07:53 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Re: Getters and setters
Well.
First question:
Im extremly confused about this.
The only usefull thing about getters that ive found is
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.Java Code:System.out.println(mesaj_2.getContent());
Last edited by Erre; 10-25-2012 at 08:03 PM.
- 10-25-2012, 08:06 PM #8
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!
- 10-25-2012, 08:19 PM #9
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
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.
Is this correct??? this is the only way i can use getters or setters??? - 2 questions, i know :P.Java Code:System.out.println(object.field)
- 10-25-2012, 08:23 PM #10
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
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.
- 10-25-2012, 08:30 PM #11
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
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
-
Confused about getters and setters
By jnjh in forum New To JavaReplies: 8Last Post: 05-03-2011, 07:12 AM -
Do getters & setters effect the performance?
By malaguena in forum New To JavaReplies: 6Last Post: 03-12-2011, 07:46 PM -
Positions and values, getters, setters
By Malus in forum New To JavaReplies: 10Last Post: 01-23-2010, 05:55 PM -
Getters and Setters
By lheviathan in forum New To JavaReplies: 4Last Post: 11-02-2009, 01:47 AM -
Getters and Setters
By Charliestons in forum New To JavaReplies: 10Last Post: 09-12-2008, 10:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks