The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes get method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "get method" Watch "get method" New topic

get method

weiliu lili
Ranch Hand

Joined: Apr 11, 2002
Posts: 46
public class Sandys{
private int court;
public static void main(String argv[]){
Sandys s = new Sandys(99);
Sandys(int ballcount){
The result:Compilation and run with an output of 99
i thought private instance variable should be matched with a get method
,in this case getCourt{},so when should i define a get method in a class?
Asif Mahmood

Joined: Jun 11, 2001
Posts: 29
Out must be 99...
court is an instance variable and is automatically initialized with 0. In the constructor of Sandys, court is initialized with 99. and no getCourt method is required.
Best regards,
Thomas Kijftenbelt
Ranch Hand

Joined: Feb 13, 2002
Posts: 73
Normally get and set methods are used to access private (encapsulated) variables from another class.
It is sorta covered in the JavaRanch Style Guide.
subject: get method
Similar Threads
private member doubt
Sample question
Marcus Green #3 Q45
Marcus Green exam question
Help for the visibility of private access modifier