Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-09-2008, 07:35 PM
Member
 
Join Date: Sep 2008
Posts: 2
Charliestons is on a distinguished road
Getters and Setters
are they really needed when u can always assign a value to ur variables?

I use Java 5.0 on eclipse... feel my pain?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-09-2008, 08:30 PM
Senior Member
 
Join Date: Aug 2008
Posts: 178
Supamagier is on a distinguished road
No, they're not.

Code:
public class Example1 { public static void main(String[] args) { Example2 e2 = new Example2(); e2.var = 56; // 1 e2.setVar(56); // 2 } } public class Example2 { public int var; public void setVar(int v) { var = v; } }
1 and 2 both work.

Is this what you mean or...?
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-09-2008, 08:46 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
get and set allow you to have change listeners and to filter the value.
Code will work with out them.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-09-2008, 10:17 PM
Member
 
Join Date: Apr 2008
Posts: 20
pheonix is on a distinguished road
setters and getters in other classes and stuff are usually used for organization, and in case you want to debug, it would be easy for you to understand. As Norm said: Code will work without them.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-10-2008, 08:03 AM
Member
 
Join Date: Aug 2008
Posts: 5
MuthuKumar is on a distinguished road
Using getter and setter- to make your variable to private .
Nobody can access it directly from outside
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-10-2008, 08:31 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
Getter and setters are a part of the standard interface for Java Beans and many frameworks like Hibernate expect them in place. That being said it is of course up to you to decide if and when you need them and for what purpose. They provide access to your private member variables and they can even give you the chance to do more than just plain get and set.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-10-2008, 10:17 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
Quote:
Originally Posted by MuthuKumar View Post
Using getter and setter- to make your variable to private. Nobody can access it directly from outside
This is correct, but there are more important ideas here. The point of OO software is reuse. This means that other programmers, or you years from now, can use the code for other systems.

When you have private member variables, and use get/set functions, you can change the internal implementation of the function without breaking all the other code that uses it.

Consider a class using a US telephone number. They are typically displayed as (nnn) nnn-nnnn such as (800) 555-1212

A naive implementation may make this just a ten character String. Or even just a String without length. And it will work.

But suppose you start to work for a cell phone company. For them, the same telephone number is not one String, its three separate and important fields:
  1. Area code
  2. exchange (aka CO)
  3. line

If you implement your class and have getPhoneNumber() and setPhoneNumber() and then implement the separate fields, you can easily add
  1. getAreaCode()
  2. setAreaCode()
  3. getExchange()
  4. setExchange()
  5. getLine()
  6. setLine()
and do all sorts of special handling, validate area codes, etc. All while

Not breaking existing users of the class.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-10-2008, 02:13 PM
Member
 
Join Date: Sep 2008
Posts: 2
Charliestons is on a distinguished road
Thanks for all d replies. I'm actually new to this whole Java thing. I use Head first java and I'm through with just the first four chapters.

Supamagier can you please explain your code.
When u say
e2.var = 56;
e2.setVar(56);

I dont quite understand that.

This forum might be extremely useful to me.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-12-2008, 07:52 AM
Member
 
Join Date: Aug 2008
Posts: 5
MuthuKumar is on a distinguished road
Thanks,
In the Hibernate, the bean(entity) - for getter/setter method. without this one, we can not map the database column name to the bean property name. This way it will helpful in the hibernate.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-12-2008, 06:45 PM
ProjectKaiser's Avatar
Member
 
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 47
ProjectKaiser is on a distinguished road
Personally I do not like getters and setters but there is nothing better ...

Java lacks explicit property declaration, like it is in .NET and Delphi platforms. The only chance is to use getters and setters for property emulation. fishtoprecords gave above very good explanation of "why to use".

Eclipse automates getters/setters creation quite well, so after some time you will get used.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 09-13-2008, 12:57 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
Quote:
Originally Posted by ProjectKaiser View Post
Personally I do not like getters and setters but there is nothing better ...

Java lacks explicit property declaration, like it is in .NET and Delphi platforms.

Eclipse automates getters/setters creation quite well, so after some time you will get used.
Personally, I really dislike properties like .net and delphi.

Netbean can also generate simple getters and setters. They work well for trivial implementations, so they are great to get started. Once you change the internals, then, of course, you have to do some actual work.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Add Fields And Generating Getter-Setters JavaForums NetBeans 0 07-31-2007 01:13 AM


All times are GMT +3. The time now is 11:23 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org