Solved

Java Constructor and Getter and Setter Methods

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
listing all functions in JavaScript 19 172
C# DataGridView_RowsAdded event not firing 1 63
program a usb light to turn on when needed 6 48
simplest php form 3 57
A short article about problems I had with the new location API and permissions in Marshmallow
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.
This video teaches viewers about errors in exception handling.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

929 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

8 Experts available now in Live!

Get 1:1 Help Now