In need of layman explanation for set/get

Tired of the technincal jargon

Page 1 of 1

7 Replies - 1042 Views - Last Post: 12 March 2008 - 09:41 AM Rate Topic: -----

#1 Trotter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 11-March 08

In need of layman explanation for set/get

Post icon  Posted 11 March 2008 - 12:34 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: In need of layman explanation for set/get

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: In need of layman explanation for set/get

Posted 11 March 2008 - 04:53 PM

Class:
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 :)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

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
Was This Post Helpful? 0
  • +
  • -

#4 Trotter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 11-March 08

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.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5909
  • View blog
  • Posts: 12,815
  • Joined: 16-October 07

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.
Was This Post Helpful? 0
  • +
  • -

#6 Trotter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 11-March 08

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?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5909
  • View blog
  • Posts: 12,815
  • Joined: 16-October 07

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.
Was This Post Helpful? 0
  • +
  • -

#8 Trotter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 11-March 08

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. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1