aspose file tools*
The moose likes Java in General and the fly likes what is getter/setter in java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is getter/setter in java ?" Watch "what is getter/setter in java ?" New topic
Author

what is getter/setter in java ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

what are getters and setters in java , i read about it but didnt understand why exactly we need it , cant we write applications with out getter/setter ?
thanks for explaining , but please use examples and simple examples
is getmethod() and setmethod are different than getter and setter


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

'getters' is a term for a method that gets the value of a variable. a setter sets the value. That's really all there is to it.

Of course you can write applications without them, but you may not want to. The idea is that you don't want all your variables to be public, because then anyone can update them without you knowing. getters and setters are used to control access.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

fred rosenberger wrote:'getters' is a term for a method that gets the value of a variable. a setter sets the value. That's really all there is to it.

Of course you can write applications without them, but you may not want to. The idea is that you don't want all your variables to be public, because then anyone can update them without you knowing. getters and setters are used to control access.

so you mean if i declare something like what i understand from this code is setName() is method which can run without a getName() like other methods
than why we write getters and setters , still i m not clear
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136

would not work

name is private... hence cant be accessed outside the class.
To understand getters and setters you need to understand the abstraction principle of OOP

There are rules for public fields. For eg, you want to impose a rule on a field (like a constraint, or read only rule or whatever). You need to have control on how to set the value and how it is accessed.
If it is made public, any application can modify it without control. Hence you need setters and getters.
For eg, lets assume your name cant have a size = 0 ("") as a name.

This way name can be set only using your methods. And a contract is established. You can also have counters which increase everytime someone accesses the variable or so.


As above now you have control over the assignment. You can do many such things. Inject side effects in assignments (though in most cases its not recommended) but its required in certain cases.
Make it thread safe.. blah blah... possibilities are endless.

As a convention. Never make any field public unless there is a reason(Most of the times there is none). Always use getters and setters
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Gaurav Raje wrote:
would not work

name is private... hence cant be accessed outside the class.
To understand getters and setters you need to understand the abstraction principle of OOP

There are rules for public fields. For eg, you want to impose a rule on a field (like a constraint, or read only rule or whatever). You need to have control on how to set the value and how it is accessed.
If it is made public, any application can modify it without control. Hence you need setters and getters.
For eg, lets assume your name cant have a size = 0 ("") as a name.

This way name can be set only using your methods. And a contract is established. You can also have counters which increase everytime someone accesses the variable or so.


As above now you have control over the assignment. You can do many such things. Inject side effects in assignments (though in most cases its not recommended) but its required in certain cases.
Make it thread safe.. blah blah... possibilities are endless.

As a convention. Never make any field public unless there is a reason(Most of the times there is none). Always use getters and setters

now i understand k getter /setter are used to check conditions before setting the values to a member variable is this right ?
if this right , then my question is , we can do the same with any other method name says
still we can check that the name should not be empty by specifying the condition what you have specified in
then , again we come to the same point after rounding around , why we use set get method ???

consider my code


here i have changed the name from setname() to only name() and also i have deleted the getname()
but still i m getting the same output as desired .
can you explain why so ???
Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

you can freely choose the name you give to the setter and getter methods. To make the code easy to read, people usually use
setNameoftheVariable and getnameoftheVariable.


Knowledge Reigns Supreme
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
naved momin wrote:now i understand k getter /setter are used to check conditions before setting the values to a member variable is this right ?

Thats just one of the uses of getters and setters. As i said there are many other thinbgs. Some times you dont do anything other than getting or setting it.

The prefix get is just a convention(though a very strictly followed one) in the java community. Technically you can have any name of your choice for the method. But every1 else will be using get and set. Hence it is best to stick with it. Furthermore, if you use some framework like the spring framework, they actually rely on the get and set methods to define beans. So it is best you use get and set.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Gaurav Raje wrote:
naved momin wrote:now i understand k getter /setter are used to check conditions before setting the values to a member variable is this right ?

Thats just one of the uses of getters and setters. As i said there are many other thinbgs. Some times you dont do anything other than getting or setting it.

The prefix get is just a convention(though a very strictly followed one) in the java community. Technically you can have any name of your choice for the method. But every1 else will be using get and set. Hence it is best to stick with it. Furthermore, if you use some framework like the spring framework, they actually rely on the get and set methods to define beans. So it is best you use get and set.

if you would have notice that my setmethod() is alone without getmethod() but still my code works and give me the appropriate output. can you explain me how it works without get() and what are getmethod() functions are
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
naved momin wrote:
if you would have notice that my setmethod() is alone without getmethod() but still my code works and give me the appropriate output. can you explain me how it works without get() and what are getmethod() functions are


The getters and setters are not any special methods for the compiler. They are plain old java methods. There is absolutely no contract for having a method. It is just something which will make your code easier to read.

In your setter, you are not throwing an exception. You are merely printing out that they are doing something wrong.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Gaurav Raje wrote:
naved momin wrote:
if you would have notice that my setmethod() is alone without getmethod() but still my code works and give me the appropriate output. can you explain me how it works without get() and what are getmethod() functions are


The getters and setters are not any special methods for the compiler. They are plain old java methods. There is absolutely no contract for having a method. It is just something which will make your code easier to read.

In your setter, you are not throwing an exception. You are merely printing out that they are doing something wrong.

ok buddy i accept what you said above but still this code compiles and give the same output as desired , and it does not have a getmethod() too
so , why we need to write that getName() or getmethod() in our class if it doesnt do anything ???

Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49


ok buddy i accept what you said above but still this code compiles and give the same output as desired , and it does not have a getmethod() too
so , why we need to write that getName() or getmethod() in our class if it doesnt do anything ???


First of all: you dont need this methods, but its good practise to use them.
Your code can compile well without both methods, or in your specific case only with a setmethod, but no getmethod.

They dont do anything? They do what you tell em to do. As people have explained above, the minimum this methods
usually do is to return the value of a variable (getmethod) or to set a variable to a new value (setmethod). This is
done via those methods to control access to the variable. How exactly you control it and what additional functions you
write into your get/setmethods is up to you.
nitin sethi
Ranch Hand

Joined: Jul 14, 2011
Posts: 59



The getter and setter methods are written in a Java pgm to conform to the JavaBeans Standards. The following is a direct quote from the K&B Study guide.

JavaBeans Standards
The JavaBeans spec is intended to help Java developers create Java components
that can be easily used by other Java developers in a visual Integrated Development
Environment (IDE) tool (like Eclipse or NetBeans). As a Java programmer, you
want to be able to use components from the Java API, but it would be great if you
could also buy the Java component you want from "Beans 'R Us," that software
company down the street. And once you've found the components, you'd like to be
able to access them through a development tool in such a way that you don't have
to write all your code from scratch. By using naming rules, the JavaBeans spec helps
guarantee that tools can recognize and use components built by different developers.
The JavaBeans API is quite involved, but you'll need to study only a few basics for
the exam.
First, JavaBeans are Java classes that have properties. For our purposes, think of
properties as private instance variables. Since they're private, the only way
they can be accessed from outside of their class is through methods in the class. The
methods that change a property's value are called setter methods, and the methods
that retrieve a property's value are called getter methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is getter/setter in java ?