What does "static" do in Java?

What does "static" do in Java? Topic: Variable case statement example
July 22, 2019 / By Elly
Question: I'm unsure as to what it's actual purpose is. What does making something "static" make it do? So you can use it in the whole class? What does that mean? An example I didn't understand: private static int count = 0 ^ doesn't this just make it an instance? I don't think I know what instances are either... But now that count is static, what can you even do with it? And why is the main() static? Please explain it in the most basic layman's terms because all the technical talk is just flying right over my head. Gosh, I wish I could award multiple best answers! Thanks for the in depth explanations, guys!
Best Answer

Best Answers: What does "static" do in Java?

Christabelle Christabelle | 10 days ago
In order to understand what "static" does, you need to understand what a "class" is. You can think of a class like a blueprint. Say, for example, you have a class called House, which holds information about the following properties: public class House {     public int numberOfWindows;     public String color;     public House(int windows, String color) {         this.numberOfWindows = windows;         this.color = color;     } } Here, we basically made a blueprint, which tells us stuff (the number of windows and the color) of a house. Now, when we actually go to "build" the house, we're instantiating an object. House myHouse = new House(4, "white"); Here, this "instance" of a House has 4 windows and is white in color. The properties numberOfWindows and color are known as "instance variables" (or member variables) of the House class, and every House we build can potentially have different properties. For example: House yourHouse = new House(2, "blue"); Here, yourHouse has 2 windows and is blue. It has a different number of windows and is a different color than myHouse. If I were to paint myHouse a different color (say I want to change it to a yellow house), I could do it and nothing would happen to yourHouse. myHouse.color = "yellow"; // myHouse is now yellow System.out.println(yourHouse.color); // yourHouse will still be blue Notice that the "color" attribute is being called on the instance object (myHouse and yourHouse). Now, where does "static" come into play? When a variable is declared as "static", that means it's a property that's shared by ALL instances of a class. No matter how many Houses you build, every one of them will all have the same property. Even if you change that property in one object, it will change that property in the rest. For example, let's extend our House class to include the following (e.g. all Houses are built by some contracting company called "Joe's Construction"): public class House {     public static String contractor = "Joe's Construction";     public int numberOfWindows;     public String color;     public House(int windows, String color) {         this.numberOfWindows = windows;         this.color = color;     } } In this case, we can do: // These will all print "Joe's Construction" System.out.println(myHouse.contractor); System.out.println( yourHouse.contractor ); System.out.println(House.contractor); Note how, in the last print statement, that the static attribute "contractor" is called on the class ("House"), not the instance object. You can do this because a static variable is the same for the entire class and its instances. Now, suppose Joe's Construction gets a new owner named Bill, who promptly renames the company to "Bill's Construction". House.contractor = "Bill's Construction"; (You can alternatively also do myHouse.contractor = "Bill's Construction"; or yourHouse.contractor = "Bill's Construction"; It doesn't matter, and you'll only need one of these statements.) If you do the above three print statements again, they will ALL now output "Bill's Construction". The static variable was updated for the class, which in turn changed the contractor variable for all its instances. This is different from an instance variable like color, where if you change the color of myHouse, it doesn't the color of yourHouse. With static variables, changing the contractor value of myHouse will also change the contractor value of yourHouse. So, when do we use static or non-static? Generally, use static if it doesn't make sense to have a certain method as part of an instance. For example, the String class in Java has both static and non-static methods. Non-static methods require you to have an instance of a String to use them, like: String abc = "abc"; abc.toUpperCase(); // turns the String instance abc to uppercase This is because it makes sense that you want to make a specific String object uppercase. On the other hand, static methods like valueOf(int i) don't require an object. You can call them like: String.valueOf(5); // turns the integer 5 into a String You're not calling valueOf on a specific instance in this case, and so it doesn't make sense to make it a non-static method. Hence, valueOf is static. Wow, that post was longer than I thought it would be. Hopefully it helped somewhat, though.
👍 168 | 👎 10
Did you like the answer? What does "static" do in Java? Share with your friends

We found more questions related to the topic: Variable case statement example

Christabelle Originally Answered: Java Programming language program help adding a static method that consolidates an account?
In your consolidate() method you want to do these steps: 1) check that the two accounts have the same name; if not, quit* 2) check that the two accounts have different account numbers; otherwise quit* 3) add the balances of the two accounts together into a new double (maybe call it newBalance) 4) come up with a new account number 5) create a new Account object with the newBalance, new account number, and the name from one of the other accounts 6) close the two old accounts 7) return the new Account object * To quit because of a problem, it looks like you should write out an error message (see how the withdraw() method does it), and then return null (since you won't have a new Account, but you have to return something from your consolidate() method). I won't write the code for you, but that's what you need to do for your new method. One more thing you'll have to figure out: how will you come up with the new account number? It should be different from either of the other two account numbers. Maybe just add them together? No, because if one of them is zero then that won't work. You'll have to be a little creative to solve that, but it's not too hard. Good luck, you can do it!
Christabelle Originally Answered: Java Programming language program help adding a static method that consolidates an account?
static does 2 things 1. it makes 1, and only 1, copy 2. it goes ahead and initializes the variable or method and puts it into RAM You have to understand Objects are created with the keyword 'new'. Car myCar = new Car(); Car corvette = new Car(); there isn't much use for static in a Car class But if you used a count to fill a parking lot full of Car(), then it makes sense to use 'static' as a global, that is shared by all instances of Car() class Car { static int COUNT; // constructor public Car() { ++COUNT; then, you can fetch it later with System.out.println( "There are " + Car.COUNT + " cars in the parking lot".); You will soon discover if you don't have 'new' but only static's that everything: all vars, all methods, all states will also have to have the access modifier 'static' also.

Aura Aura
An instance is a data object based on a class. Say you use this piece of code: // create empty variable that can point to Scanner instances Scanner keyboard; // create an new instance of the Scanner class and make the Scanner variable called "keyboard" point to it keyboard = new Scanner(System.in); We can now use our freshly created instance in our program like this: String name = keyboard.nextLine(); What "static" does is tell Java that the variable or function is a "child" of the class itself. Not using "static" tells Java that the variable is a child of the instances created, based on the class. Example: class Player { static int count = 0; int age; } // in some other method: Player p1 = new Player(); p1.age = 23; Player.count++; Player p2 = new Player(); p2.age = 25; Player.count++; As you can see: .age is used after instances of the Player class, which are here stored in pointers p1 and p2, while count is static and thus only "Player.count" exists. Edit: just saw your question in the comment: The String class has no way of knowing about the instances you've created; you'd have to call .setFormat() on each individual instance.
👍 60 | 👎 4

Aura Originally Answered: What are some machines that use static electricity?
Emily, you have been asked to make a list of five machines that USE static electricity. Such a list is difficult to make compared to a list of machines that MAKE static electricity or PREVENT static electricity. 1. Xerography (which you have rejected). 2. Electrostatic precipitators (they are used to clean solid particles from smoke). 3. Spray painting 9 (the paint is charged to one polarity and the car to the other polarity). 4. Static adhesion (using static charges to temporarily hold objects together). 5. Ink jet printers (just like spray painting). 6. Dryness testing instruments (if the material is wet, it will not hold a static charge very well).
Aura Originally Answered: What are some machines that use static electricity?
Some air purifiers use static charges to clean the air (air ionizers). They also use static charges for painting applications to minimize over spray. I thought static might be used in certain types of scrubbers and smokestacks for pollution control, but I don't have any experience with that and don't know if it's still a common method.

If you have your own answer to the question variable case statement example, then you can write your own version, using the form below for an extended answer.