To access any of the fi eld or method the dot operator can be used. For example, to access numand
show in the earlier class example the following can be written:
demo dd=new demo();
dd.num=20;
dd.show();
7.2 PROGRAMMING EXAMPLES
/*PROG 7.1 DEMO OF CLASS VER 1 */
class demo
{
private int cx, cy;
void input(int x, int y)
{
cx = x;
cy = y;
}
void show()
{
System.out.println(“cx=” + cx);
System.out.println(“cy=” + cy);
}
}
class JPS2
{
public static void main(String args[])
{
demo d = new demo();
d.input(10, 20);
d.show();
}
}
OUTPUT:
cx=10
cy=20 1
Explanation:A class is created ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.