Java find output programs (static Keyword) | set 2

Find the output of Java programs | static Keyword | Set 2: 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:

static 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:

/StaticEx.java:1: error: modifier static not allowed here
static class Sample {
       ^
1 error

Explanation:

The above program will generate a syntax error because here we created the Sample class as a static, but we cannot create a static class in java.

Question 2:

class Sample {
  static int count;

  static 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:4: error: modifier static not allowed here
  static Sample() {
         ^
1 error

Explanation:

The above program will generate syntax error because we cannot create a static constructor in java. Here, we created a static constructor in Sample class.

Question 3:

class Customer {
  static int BankBalance;

  int balance;
  Customer(int bal) {
    balance = bal;
    BankBalance += balance;
  }

  static void printBalance() {
    System.out.println("Customer Balance: " + balance);
    System.out.println("Bank Balance: " + BankBalance + "\n");
  }
}

public class StaticEx {
  public static void main(String[] args) {
    Customer C1 = new Customer(1000);
    C1.printBalance();

    Customer C2 = new Customer(2000);
    C2.printBalance();
  }
}

Output:

/StaticEx.java:11: error: non-static variable balance cannot be 
referenced from a static context
    System.out.println("Customer Balance: " + balance);
                                              ^
1 error

Explanation:

The above program will generate a syntax error because we used non-static data member balance in static method printBalance().

Question 4:

class Customer {
  static int BankBalance;

  int balance;
  Customer(int bal) {
    balance = bal;
    BankBalance += balance;
  }

  void printBalance() {
    System.out.println("Customer Balance: " + balance);
    System.out.println("Bank Balance: " + BankBalance + "\n");
  }
}

public class StaticEx {
  public static void main(String[] args) {
    Customer C1 = new Customer(1000);
    C1.printBalance();

    Customer C2 = new Customer(2000);
    C2.printBalance();
  }
}

Output:

Customer Balance: 1000
Bank Balance: 1000

Customer Balance: 2000
Bank Balance: 3000

Explanation:

In the above program, we created a Customer class that contains a non-static member balance and a static member BankBalance. Here, we created one constructor and printBalance() method.

The constructor of Customer class is used to initialize data member balance and add balance value to the static variable BankBalance. The static member BankBalance represents the sum of all balances for all customers.

Now look to the main() method, here we created the objects C1 and C2 of customer class and initialized each object of Customer class with some initial value. Then print the balance of the customer as well as print the bank balance on the console screen.

Question 5:

class Customer {
  static int BankBalance;

  int balance;
  Customer(int bal) {
    balance = bal;
    BankBalance += balance;
  }

  void printBalance() {
    System.out.println("Customer Balance: " + balance);
    System.out.println("Bank Balance: " + BankBalance + "\n");
  }
}

public class StaticEx {
  public static void main(String[] args) {
    static Customer C1 = new Customer(1000);
    C1.printBalance();

    static Customer C2 = new Customer(2000);
    C2.printBalance();
  }
}

Output:

/StaticEx.java:18: error: illegal start of expression
    static Customer C1 = new Customer(1000);
    ^
/StaticEx.java:19: error: <identifier> expected
    C1.printBalance();
                   ^
/StaticEx.java:22: error: <identifier> expected
    C2.printBalance();
                   ^
/StaticEx.java:24: error: class, interface, or enum expected
}
^
4 errors

Explanation:

The above program will generate syntax errors because of the below statements,

static Customer C1 = new Customer(1000);
C1.printBalance();
        
static Customer C2 = new Customer(2000);
C2.printBalance();

In the above statements, we created static objects but we cannot create static objects in the java.






Comments and Discussions!

Load comments ↻






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