[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Constructor and Getter and Setter Methods

Posted on 2013-01-03
5
Medium Priority
?
968 Views
Last Modified: 2013-01-03
Hello

I have a quick question regarding Java Constructor and Getter and Setter Methods.

Do you have to declare and  initialize both a constructor and getter and setter methods in your class. Or can you choose one or the other?

Thanks
0
Comment
Question by:Kevin_Coors
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
krakatoa earned 1020 total points
ID: 38742179
You don't need any of them if you don't want to.

You "get" a default constructor from the JVM when you instantiate a class object. Obviously the default ctr is not retro-fitted into your code - you don't see it, it just gets implemented. Getters and Setters are to adhere to OOP principles, whereby you should have only private variables in a class, and have them interrogated and modified by other class objects via method calls, so as not to create dependencies.
0
 

Author Comment

by:Kevin_Coors
ID: 38742207
Hey Krakatoa

Sorry, If the class variables are private I mean.

I know how to use the dot  notation to  call a class variable i.e. var.name = "John" but I have read that this is poor practice for encapsulation.

Does Encapsulation require a constructor followed by getter and setter methods?
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 38742241
but I have read that this is poor practice for encapsulation.
Depends who you ask I reckon. Dot notational access implies leaving the back door open for anyone who wants a cup of sugar. Getter method will pass you the sugar through a hatch.

See this.
0
 

Author Comment

by:Kevin_Coors
ID: 38742264
great website!

thanks again
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 38742271
Cool.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question