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

Question #3475
(1) Write a program to declare any variables as public and access them in another class as you seen in the examples?
1
Expert's answer
2011-08-07T07:13:53-0400
public class Task6
{
public static void main(String[] args)
{
& Rectangle r1 = new Rectangle(5, 10);

& // The access to the public variables
& System.out.print("Rectangle: width = " + r1.width + ", height = " + r1.height);
}
}

class Rectangle
{
// These variables are public so we can access them anywhere
public int width;
public int height;

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

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
APPROVED BY CLIENTS