954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

PlayClass

0
By Dani Horowitz on Sep 20th, 2004 8:18 pm

Two classes which aim to demonstrate private and public members of a class. A static variable is used as an accumulator to keep track of the number of class objects which are created.

package playclass;
public class PlayClass
{
// public static int j = 30;
private int j = 30;
private static int nobjects = 0;
public PlayClass() { nobjects++; }
public PlayClass(int l)
{
j = l;
nobjects++;
}
public static void main(String args[])
{
MyClass object1 = new MyClass();
MyClass object2 = new MyClass(3);
MyClass object3 = new MyClass(5);
System.out.print("\n\t i = " + object1.getI());
System.out.print("\n\t i = " + object2.getI());
// static double z = 2.3;
// j++;
PlayClass myObject1 = new PlayClass();
PlayClass myObject2 = new PlayClass(35);
System.out.print("\n\t j in PlayClass = " + myObject1.j);
System.out.print("\n\n\tNumber of objects of MyClass = " +
MyClass.getNobjects());
System.out.print("\n\n\tNumber of objects of PlayClass = " +
PlayClass.nobjects);
// MyClass.nobjects = 320;
}
}
class MyClass
{
private int i = 10;
private static int nobjects = 0;
MyClass() { nobjects++; }
MyClass(int li)
{
i = li;
nobjects++;
}
public int getI() { return i; }
public static int getNobjects() { return nobjects; }
}

java
Comments: 0 | Views: 769 | Sep 20th 2004 03:18 PM

Two classes which aim to demonstrate private and public members of a class. A static variable is used as an accumulator to keep track of the number of class objects which are created.

package playclass;
public class PlayClass
{
// public static int j = 30;
private int j = 30;
private static int nobjects = 0;
public PlayClass() { nobjects++; }
public PlayClass(int l)
{
j = l;
nobjects++;
}
public static void main(String args[])
{
MyClass object1 = new MyClass();
MyClass object2 = new MyClass(3);
MyClass object3 = new MyClass(5);
System.out.print("\n\t i = " + object1.getI());
System.out.print("\n\t i = " + object2.getI());
// static double z = 2.3;
// j++;
PlayClass myObject1 = new PlayClass();
PlayClass myObject2 = new PlayClass(35);
System.out.print("\n\t j in PlayClass = " + myObject1.j);
System.out.print("\n\n\tNumber of objects of MyClass = " +
MyClass.getNobjects());
System.out.print("\n\n\tNumber of objects of PlayClass = " +
PlayClass.nobjects);
// MyClass.nobjects = 320;
}
}
class MyClass
{
private int i = 10;
private static int nobjects = 0;
MyClass() { nobjects++; }
MyClass(int li)
{
i = li;
nobjects++;
}
public int getI() { return i; }
public static int getNobjects() { return nobjects; }

tunstar
Newbie Poster
1 post since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You