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
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) 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...
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 Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks the...
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.
For Java. Let's get some practice using Java libraries! Create a class named Formatter. Formatter should...
For Java. Let's get some practice using Java libraries! Create a class named Formatter. Formatter should provide one public class method formatDate. It should accept a positive long representing milliseconds since 1970 and return a String containing the ISO-8601 representation of this time. Here is one example for a recent timestamp: given 1602106609897 your function should return 2020-10-07T21:36:49.897Z. Do not overthink this. The solution we are after is a single line of code. We suggest that you explore the various...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
What is the result of 2^7 in Java? Why does it give this result? What would...
What is the result of 2^7 in Java? Why does it give this result? What would you use to get the result you would have expected before taking this class?
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a sub class's constructor? Select one: a. It must be the first statement in the constructor b. If you don't include it Java will c. If you don't include it you must have a 0 parameter constructor coded in the super class or no constructors coded at all in the super class d. The sub class constructor containing the super call and the called super...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT