JAVA QUESTION: How are static fields and methods different from non-static fields and methods?

by Scott O: QUESTION: How are static fields and from non-static fields and methods?
How are static fields and methods different from non-static fields and methods? What would go wrong if we declared the balance field of a bank account class to be static? What could go wrong if every account had a field containing today's ?

Best answer:

Answer by me
Static fields and methods belong to the class not the individual instances of the class. All instances of a given class share the same static variables/methods.

For example:

public class Dog {
public static String breed;
public String name;


public class StaticTest {

public static void main(String[] args) {
Dog dog1 = new Dog(); = “Spot”;
dog1.breed = “ ”;

Dog dog2 = new Dog(); = “Fido”;
dog2.breed= “Mutt”;

System.out.println( + ” is a ” + dog1.breed);
System.out.println( + ” is a ” + dog2.breed);

When run, you'll get:

Spot is a Mutt
Fido is a Mutt

Because while name belongs to each individual instance of Dog, breed is shared among all Dog's

Hopefully you can see the disastrous results of making checking account balance a static attribute. Everyone at the bank would always have the same amount of money.

On the other hand, keeping today's interest rate in each account is too much overhead since the interest rate should be shared by all customers.

Static methods also work well for functions that don't necessarily require an object to be created in order to run that function.

For example:

public static long farenheitToCelcius(int F, int C);
public static final String RGB_BLUE = “#0000FF”;

could be called from anywhere without have to instantiate their parent objects.

Add your own answer in the comments!

Bestsellers and Hot Products

You May Also Like These Topics...

The Czech Shepherd

The Czech “German” Shepherd working line is not your traditional . Although they certainly still have the typical face and stance, they are generally smaller (with some exceptions of course) and have a wider variation in coat and color. They contrast most with the typical American show lines and the general idea of how […]

German Shepherd Nutritional Needs

A German Shepherd is a strong, athletic breed and their energetic nature means that need a diet that is packed with nutrients. Being carnivores, they need higher protein content in their diet to keep them healthy. The recommended protein content is 22% with a certain amount of fat that will keep your German Shepherd loving […]

What the Heck is a Gerberian Shepsky?

Quite frankly, I've never heard that one before. Sure, we all kinds of doodle and -oodle and -poo designer breeds, and I've even heard of some -inu and quite a few -eagle breeds as well. Though, actually they are not breeds per se, but “hybrids”. That is – until they start breeding true after a certain […]

German shepherd training ? Stop german shepherd puppy chewing

by TaranRampersad The German Shepherd is an AKC registered dog breed. The following article reviews the breed. If you're trying to stop german shepherd chewing, you must first recognize that biting is a very pure and expected habits in a german . They will chew on points, either because they are bored or […]

Tags: , , , , , , , , , , , , ,
Previous Post
German Shepherd

German Shepherd – a Different View

Next Post
German Shepherd

Vintage Metal German Shepherd Rin Tin Tin Dog Toy Figure Figurine Made in Japan

Leave a Reply