Question

java Give an example of a class that contains at least one class variable and at...

java

Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.

Homework Answers

Answer #1

ClassVarTest.java


public class ClassVarTest {

   public static int count = 0;
   public static void main(String[] args) {
       ClassVarTest c1 = new ClassVarTest();
       ClassVarTest c2 = new ClassVarTest();
       c1.increaseCount();
       System.out.println(count);
       c2.increaseCount();
       System.out.println(count);
   }
   public static void increaseCount(){
       count++;
   }
}

Output:

1

2

Here the class variable is "count" and class method is "increaseCount()"

Class variable and class methods are nothing but a static variables and static methods.

Class level ariable are accessed by all instance of the same class. It means same memory will be used for each of instance of a class. Some If once instance changes the value of classs variable that value is autmatically effects to the rest of all instances.

In our case, I am increasing count variable value in class method. When we use instance c1 that cout value is 1 and whn we use instance c2 count variable value is 2.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a Shape (Object) Class Create a class (Shapes) that prompts the user to select a...
Create a Shape (Object) Class Create a class (Shapes) that prompts the user to select a shape to be drawn on the screen. Objects: Circle, X, box, box with an x inside, or any other object of your choice. Depending on the user’s choice, you will then ask for the number of rows, columns or any requirements needed to draw the shape. Finally, you will display the shape selected by the user to the screen. Your class should have at...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
Java programming 1) Which among the choices is the following Java expression equivalent to? !((b !=...
Java programming 1) Which among the choices is the following Java expression equivalent to? !((b != 0) || (c <= 5)) Select one: a)       (b != 0) && (c <= 5) b)       (b == 0) && (c > 5) c)       !((b <> 0) && (c <= 5)) d)       (! (b = 0)) && (! (c > 5)) e)       (b == 0) && (c <= 5) 2) The scope of a variable is an important feature in Java, the following variable...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
use an example to declare a generic class at least one generic method
use an example to declare a generic class at least one generic method
Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class...
Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class ( super class) 2,3. Create two sub classes ( car, bus , truck train or any) for vehicle class 4 Create a subclass (sportscar or schoolbus or Goodstrain or any) for car or bus You need to use the following atleast for one class. Use a protected variable Add constructor for superclass and subclass Override a method which displays a description about the vehicle...
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x)...
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x) { switch (x) { case 1: break; case 2: break; case 0: break; default: break; case 4: break; } } Select one: a)Each case section must end with a break statement. b)The case label 0 must precede case label 1. c)There is nothing wrong with the Java code. d)The default label must be the last label in the switch statement. e)The variable x does...
Give an example of a connected undirected graph that contains at least twelve vertices that contains...
Give an example of a connected undirected graph that contains at least twelve vertices that contains at least two circuits. Draw that graph labeling the vertices with letters of the alphabet. Determine one spanning tree of that graph and draw it. Determine whether the graph has an Euler circuit. If so, specify the circuit by enumerating the vertices involved. Determine whether the graph has an Hamiltonian circuit. If so, specify the circuit by enumerating the vertices involved.
You are given the following Java files: TestCharacterStack.java that contains class LLStack with class stackNode listed...
You are given the following Java files: TestCharacterStack.java that contains class LLStack with class stackNode listed as inner class.    You are given the following Java file: TestCharacterStack.java inside the class add the method below:         public boolean ParenthesesMatching (String text) takes a string text, and parenthesis whether its parenthesis are "balanced." Hint: for left parenthesis, push onto stack; for right parenthesis, pop from stack and check whether popped element matches right parenthesis, else return false. If returned value is...