The title says it best. I am taking a basic Java class through Axia. My last assignment (Payroll Project 3) made next to no sense to me. I turned in the assignment, but I have very little idea if it was what I was supposed to do. My class was set up properly, but my set/get stuff was whack.
All I am looking for is an explanation of how they work, and an example or two so I can see them in action. I don't need a lot of technical smoke and mirors... our book is chock full of all of that, and that is why I am here. I will probably have a lot of questions before I am done, so you all stand warned.
In need of layman explanation for set/getTired of the technincal jargon
Page 1 of 1
7 Replies - 882 Views - Last Post: 12 March 2008 - 09:41 AM
Replies To: In need of layman explanation for set/get
#2
Re: In need of layman explanation for set/get
Posted 11 March 2008 - 04:53 PM
Class:
Test:
If you still have question keep posting
public class SetGet {
//private member fields can not be accessed outside this class
private int x;
private char c;
private String name;
//So we use the 'public' methods called Getters and Setters
public char getC() {
return c;
}
public void setC(char c) {
this.c = c;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
Test:
public class SetGetMain {
public static void main(String[] args) {
SetGet test = new SetGet(); //the default constructor of the SetGet Class
//not vaild statements, private memeber fields can not be accessed directly
test.c = 'a';
test.x = 15;
test.name = "MyName";
System.out.println(test.c+" "+test.x+" "+test.name); //again can not access these fields
//can be changed using public methods
test.setC('a');
test.setX(15);
test.setName("MyName");
//To view their currect values for the object 'test' of class SetGet
System.out.println(test.getC()+" "+test.getName()+" "+test.getX());
//So these are some examples to access private memeber fields using set and get
}
}
If you still have question keep posting
#3
Re: In need of layman explanation for set/get
Posted 11 March 2008 - 04:55 PM
In layman's terms:
Get = retrieve the value of the property
Set = set the value of the property
Get = retrieve the value of the property
Set = set the value of the property
#4
Re: In need of layman explanation for set/get
Posted 11 March 2008 - 07:12 PM
OK. I was thinking that set was to store the value and get was to retrieve it. nice to know I finally got that part right. Would have been nice to know before my last assignment, though.
Rather than start a new thread, I'll ask another one... Explain to me what the actual difference is between public and private. I know Private keeps the information inside whatever, and public doesn't, but what are the rules and how does it work? I see them used, but I don't get the how and why.
Rather than start a new thread, I'll ask another one... Explain to me what the actual difference is between public and private. I know Private keeps the information inside whatever, and public doesn't, but what are the rules and how does it work? I see them used, but I don't get the how and why.
#5
Re: In need of layman explanation for set/get
Posted 11 March 2008 - 08:15 PM
Keep in mind that getter and setter methods are a convention, they aren't required and no linguistic rules exist to enforce any kind of behavior, they're just generally used in the way described.
Element access levels, public, private, protected, are actually a reasonable segue. In most classes, variables should always be private and thus to the need, in some cases, for getters and setters to access such values.
Public means that the class allows outside objects to freely access something. From a design standpoint, this shows the user of the class which methods are available to them. More importantly, it allows all the private code of the class to be changed as needed without worry of other classes. As long as all the public elements behave consistently, private stuff can be changed without impacting anything else.
Element access levels, public, private, protected, are actually a reasonable segue. In most classes, variables should always be private and thus to the need, in some cases, for getters and setters to access such values.
Public means that the class allows outside objects to freely access something. From a design standpoint, this shows the user of the class which methods are available to them. More importantly, it allows all the private code of the class to be changed as needed without worry of other classes. As long as all the public elements behave consistently, private stuff can be changed without impacting anything else.
#6
Re: In need of layman explanation for set/get
Posted 12 March 2008 - 05:56 AM
Thanks, Baavgai.
See if I have this straight... Private is just that, private. it only exists inside the class it is created in, and that is why you have to have the get command. Public is public, accessable by everything. private can only be changed within its class, while public can be changed by anything anywhere. Am I close?
See if I have this straight... Private is just that, private. it only exists inside the class it is created in, and that is why you have to have the get command. Public is public, accessable by everything. private can only be changed within its class, while public can be changed by anything anywhere. Am I close?
#7
Re: In need of layman explanation for set/get
Posted 12 March 2008 - 06:40 AM
You got it, pretty much.
There is a third state, protected. This essentially means that private to the world, but public the classes that extend it. Some programs seem to avoid protected, perhaps because they don't understand it. If you're writing a number of classes, chances are there a point it will make sense to reveal something to the child classes that you don't want to share with the world.
And that's it, three access levels. Every OO language has the idea of public and private, though in practice sometimes it's all public. Most have the idea protected. Some take that down farther, like .NETs internal. But they're variations on the theme; who can see to code and who can't.
It may seem like just extra work for a small program, but on large systems this stuff helps a lot.
There is a third state, protected. This essentially means that private to the world, but public the classes that extend it. Some programs seem to avoid protected, perhaps because they don't understand it. If you're writing a number of classes, chances are there a point it will make sense to reveal something to the child classes that you don't want to share with the world.
And that's it, three access levels. Every OO language has the idea of public and private, though in practice sometimes it's all public. Most have the idea protected. Some take that down farther, like .NETs internal. But they're variations on the theme; who can see to code and who can't.
It may seem like just extra work for a small program, but on large systems this stuff helps a lot.
#8
Re: In need of layman explanation for set/get
Posted 12 March 2008 - 09:41 AM
Thanks. If you can believe, the answers really help me get a grasp on all of this. I will begin work on my next assignment this evening, so I know I will be using this stuff. And, yes, I am in the Payroll/Inventory route. I start the Inventory routing on this assignment.
I'm not too worried about this one, as it is pretty straight forward. But if I don't get all of this straight in my mind now, i will be royally screwed later on.
I will return with more questions later.
I'm not too worried about this one, as it is pretty straight forward. But if I don't get all of this straight in my mind now, i will be royally screwed later on.
I will return with more questions later.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|