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();
}
}
Answer Question 1:
Output:
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:
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:
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:
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:
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