Q:

Java find output programs (static Keyword) | set 1

belongs to collection: Java find output programs

0

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.

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

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

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

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

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

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();

Answer Question 2:

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.

Answer Question 3:

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.

Answer Question 4:

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.

Answer Question 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (static Keyword) | set 2... >>
<< Java find output programs (final Keyword) | set 2...