Avatar of Kevin_Coors
Kevin_Coors
 asked on

Java Constructor and Getter and Setter Methods

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
JavaProgramming

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
krakatoa

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kevin_Coors

ASKER
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?
krakatoa

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.
Kevin_Coors

ASKER
great website!

thanks again
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
krakatoa

Cool.