Solved

Java Constructor and Getter and Setter Methods

Posted on 2013-01-03
5
905 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 16

Accepted Solution

by:
krakatoa earned 255 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 16

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 16

Expert Comment

by:krakatoa
ID: 38742271
Cool.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now