Answer to Question #3474 in Java | JSP | JSF for Y mukundareddy

Question #3474
(2) Write a program to declare any variables as private in the same class and access them in the same class?
1
Expert's answer
2011-08-07T07:15:24-0400
public class Task5
{
public static void main(String[] args)
{
& Rectangle r1 = new Rectangle(5, 10);
& r1.printInfo();
}
}

// Class with private members. The function printInfo() of the same class
// has access to this members
class Rectangle
{
// These variables are private, so we can access them only in the same class
private int width;
private int height;

Rectangle(int pWidth, int pHeight)
{
& width = pWidth;
& height = pHeight;
}

public void printInfo()
{
& System.out.print("Rectangle: width = " + width + ", height = " + height);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS