Q:

Java find output programs (static Keyword) | set 2

belongs to collection: Java find output programs

0

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.

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

  }
}

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

  }
}

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

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

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

All Answers

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

Answer Question 1:

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.

Answer Question 2:

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.

Answer Question 3:

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

Answer Question 4:

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.

Answer Question 5:

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.

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 (Parameter Passing) | se... >>
<< Java find output programs (static Keyword) | set 1...