PlayClass

Updated Dani 0 Tallied Votes 252 Views Share

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 0 Newbie Poster

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; }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.