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; }
}

While pursuing a Computer Science degree, I founded DaniWeb.com, an online community for developers and IT professionals. I coded the backend platform from the ground up and I also do all of the advertising sales and SEO. I'm a super-geeky programmer with a passion for Internet marketing.

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; }

Edited 3 Years Ago by pyTony: fixed formating

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.