The Artima Developer Community
Sponsored Link

Java Answers Forum
Constructor, setters and getters

2 replies on 1 page. Most recent reply: May 9, 2009 3:51 PM by Timothy Hoyle

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Timothy Hoyle

Posts: 2
Nickname: timdaslim
Registered: Apr, 2009

Constructor, setters and getters Posted: May 3, 2009 12:05 PM
Reply to this message Reply
Advertisement
Ok, I am an green Java programmer in a Java class for college (online) I am having trouble getting my instructor to respond to my questions so I figure that I will ask them here.

If we initialize variables via a constructor such as:

private String firstName
private String lastName

public employee (String first, String last)


Why do we include setter methods that initialize the string parameters "first" and "last" to "firstName" and "lastName"?

Why do we not just initialize these variables via the constructor with the same variable names("firstName" and "lastName" instead of "first" and "last") in the first place?

Would the constructor not assign these values to the correct variables anyways so we could just skip the "setter" methods and go straight to creating "getter" methods to access these variable values?


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Constructor, setters and getters Posted: May 4, 2009 11:43 PM
Reply to this message Reply
> Ok, I am an green Java programmer in a Java class for
> college (online) I am having trouble getting my instructor
> to respond to my questions so I figure that I will ask
> them here.
>
> If we initialize variables via a constructor such
> such as:
>
> private String firstName
> private String lastName
>
> public employee (String first, String last)
>
>
> Why do we include setter methods that initialize the
> e the string parameters "first" and "last" to "firstName"
> and "lastName"?
>
> Why do we not just initialize these variables via the
> a the constructor with the same variable names("firstName"
> and "lastName" instead of "first" and "last") in the
> first place?
>
> Would the constructor not assign these values to the
> correct variables anyways so we could just skip the
> "setter" methods and go straight to creating "getter"
> methods to access these variable values?

You can initialize the variables via a Constructor, but in the case of Getters and Setters they are really accessor methods to your object. You could still access your variables via reflection (but that's a different path for frameworky stuff).
Employee emp = new Empoyee("Someone", "Surname");
//  now firstName = "Someone" and lastName = "Surname"

Now say the last name has changed, do you really want to go and instantiate emp again?
emp.setLastName("NewMarriedName");

Timothy Hoyle

Posts: 2
Nickname: timdaslim
Registered: Apr, 2009

Re: Constructor, setters and getters Posted: May 9, 2009 3:51 PM
Reply to this message Reply
Thanks for the response Kondwani.

So what your saying is that the setter methods are there just for future manipulation of individual variables.

Meaning, you declare the initial variable values via the constructor when you instantiate the object. But, to go in later and manipulate an individual variable you use the setter method.

This is the alternative to destroying an object and re-instantiating it just to change one variable's value.

Flat View: This topic has 2 replies on 1 page
Topic: Unsupported major.minor version 49.0 Previous Topic   Next Topic Topic: Question in Java Programming(GUI)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use