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?
Expert's answer
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!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog