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

array(2) { [0]=> string(0) "" ["keywords"]=> string(17) "Different,fields," }

Question by Scott O: JAVA QUESTION: How are and methods different from and methods?
How are 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 interest rate?

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 class StaticTest {

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

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

System.out.println(dog1.name + ” is a ” + dog1.breed);
System.out.println(dog2.name + ” 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);
and
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!


You May Also Like These Topics...

The Czech Shepherd

array(2) { [0]=> string(0) "" ["keywords"]=> string(28) "dog breeds,German Shepherds," }

The Czech “German” Shepherd working line is not your traditional German Shepherd. 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

array(2) { [0]=> string(0) "" ["keywords"]=> string(25) ",German Shepherd," }

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?

array(2) { [0]=> string(0) "" ["keywords"]=> string(36) "american kennel club,designer breed," }

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

array(2) { [0]=> string(0) "" ["keywords"]=> string(15) ",German," }

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

Tags: , , , , , , , , , , , , ,
Previous Post
3280180110_50e2bac11f_m
German Shepherd

German Shepherd – a Different View

Next Post
41KwYhHmURL._SL75_4
German Shepherd

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

Leave a Reply

0 Shares
Tweet
Share
Share
Pin