Java find output programs (static Keyword) | set 1

Find the output of Java programs | static Keyword | Set 1: Enhance the knowledge of Java static Keyword concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 02, 2021

Question 1:

class Sample {
  static int X;
  static int Y;

  Sample() {
    X = 10;
    Y = 20;
  }
  static void staticFun() {
    int Z = 0;

    Z = ++X + Y * 2;

    System.out.println(X + " " + Y + " " + Z);
  }
}

public class StaticEx {
  public static void main(String[] args) {
    Sample::staticFun();
  }
}

Output:

/StaticEx.java:20: error: not a statement
    Sample::staticFun();
    ^
/StaticEx.java:20: error: ';' expected
    Sample::staticFun();
                     ^
2 errors

Explanation:

The above program will generate syntax errors because we cannot access static methods using "::" operator. Here, we need to use '.' operator to access static methods.

The correct way to call the static method is given below:

Sample.staticFun();

Question 2:

class Sample {
  int X;
  int Y;

  Sample() {
    X = 10;
    Y = 20;
  }
  static void staticFun() {
    int Z = 0;

    Z = ++X + Y * 2;

    System.out.println(X + " " + Y + " " + Z);
  }
}

public class StaticEx {
  public static void main(String[] args) {
    Sample.staticFun();
  }
}

Output:

/StaticEx.java:12: error: non-static variable X cannot be 
referenced from a static context
    Z = ++X + Y * 2;
          ^
/StaticEx.java:12: error: non-static variable Y cannot be 
referenced from a static context
    Z = ++X + Y * 2;
              ^
/StaticEx.java:14: error: non-static variable X cannot be 
referenced from a static context
    System.out.println(X + " " + Y + " " + Z);
                       ^
/StaticEx.java:14: error: non-static variable Y cannot be 
referenced from a static context
    System.out.println(X + " " + Y + " " + Z);
                                 ^
4 errors

Explanation:

The above program will generate syntax errors because here we accessed non-static data members X and Y from static method staticFun(). We cannot access non-static members from static members of the class.

Question 3:

class Sample {
  private static int count;

  private Sample() {
    count++;
  }

  static void ObjectCount() {
    System.out.println(count);
  }

}

public class StaticEx {
  public static void main(String[] args) {
    Sample S1 = new Sample();
    Sample S2 = new Sample();
    Sample S3 = new Sample();

    Sample.ObjectCount();
  }
}

Output:

/StaticEx.java:16: error: Sample() has private access in Sample
    Sample S1 = new Sample();
                ^
/StaticEx.java:17: error: Sample() has private access in Sample
    Sample S2 = new Sample();
                ^
/StaticEx.java:18: error: Sample() has private access in Sample
    Sample S3 = new Sample();
                ^
3 errors

Explanation:

The above program will generate syntax errors because here we declared constructor of Sample class as a private, and we created the object of the Sample class in main() method of StaticEx class and we know that we cannot access private members outside the class.

Question 4:

class Sample {
  private static int count;

  Sample() {
    count++;
  }

  static void ObjectCount() {
    System.out.println(count);
  }

}

public class StaticEx {
  public static void main(String[] args) {
    Sample S1 = new Sample();
    Sample S2 = new Sample();
    Sample S3 = new Sample();

    Sample.ObjectCount();
  }
}

Output:

3

Explanation:

In the above program, we created a Sample class that contains a static data member count and a no-argument constructor and a static method ObjectCount().

Here, we increased the value of static variable count in the constructor that will count the total created objects. The ObjectCount() method is used to print the total number of objects created.

Question 5:

class Sample {
  private static int count;

  Sample() {
    count++;
  }

  static void ObjectCount() {
    System.out.println(count);
  }

}

public class StaticEx {
  public static void main(String[] args) {
    Sample S1 = new Sample();
    Sample S2 = new Sample();
    Sample S3 = new Sample();

    S1.ObjectCount();
    S2.ObjectCount();
    S3.ObjectCount();
  }
}

Output:

3
3
3

Explanation:

In the above program, we created a Sample class that contains a static data member count and a no-argument constructor and a static method ObjectCount().

Here, we increased the value of static variable count in the constructor that will count the total created objects. The ObjectCount() method is used to print the total number of objects created.

Now look to the main() method, here we created three objects of Sample class and call ObjectCount() method with each object then "3" will be printed each time.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.