Untitled

Anonymous
Plain Text
09/21/2025 4:53 AM
942 B
16
Indexable
Never
class StaticThisDemo {
    // Static variable
    static int staticCount = 0;
    // Instance variable
    int value;

    // Constructor using 'this' keyword
    public StaticThisDemo(int value) {
        this.value = value; // 'this' refers to the current object
        staticCount++;      // Increment static variable for each object created
    }

    // Static method
    static void showStaticCount() {
        System.out.println("Number of objects created: " + staticCount);
    }

    // Instance method
    void showValue() {
        System.out.println("Value for this object: " + this.value);
    }

    
}
public class a {
public static void main(String[] args) {
   StaticThisDemo obj1 = new StaticThisDemo(10);
        StaticThisDemo obj2 = new StaticThisDemo(20);

        obj1.showValue();
        obj2.showValue();

        StaticThisDemo.showStaticCount(); // Call static method

    }}

Editor is loading...
Leave a Comment