2

So I have a question regarding best practices. Basically I'm doing the following to make accessing members of different classes easier:

class myClass1 {

public static int var1; 
public static String var2; 

//...

public static void method1() {
//...
}

}

And then in other classes I can just access myClass1 members with myClass1.var1, myClass1.var2, myClass1.method1(). Another design pattern I see is to not use static at all and just do myClass1 instance = new myClass1(); and then do instance.method1(); or whatever.

I remember hearing something somewhere about static being bad... relating to global objects or whatever. But it's been a while since intro to computer science, heh.

Anyways, beginner Java programmer just looking to get some insight into best practices. Thanks.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
JDS
  • 16,388
  • 47
  • 161
  • 224
  • It might be best to create a better example for yourself than myClass1(). You may want to try model something that might amuse you like an application that models players/levels/rules of a game. You might be able to see a little more into your question. – dseibert Jun 17 '11 at 21:00
  • Checkout this post: http://stackoverflow.com/questions/1561299/overuse-of-static-in-my-java-game-project – Kevin Bowersox Jun 17 '11 at 20:57

6 Answers6

8

The semantics of static vs. non-static member variables and methods are completely different. Non-static variables are members of instances of the class; each instance has its own copy. Static variables are members of the class itself; they're not tied to any particular instance.

Similarly, non-static methods operate on instances of the class, static methods aren't tied to a particular instance.

You should use static/non-static as the problem requires. This isn't a question of best practices.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
3

In general having all the members fields/methods public static is considered bad practice for Object Oriented Programming paradigm. It takes all the notions of object encapsulation and data security. Any client of your class is free to tamper your data any time. This kind of practice is very similar to defining global variables and functions in procedural languages like C.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    You're describing an issue with `public`, not `static`. This question is comparing `static` to non-`static`. – Oliver Charlesworth Jun 17 '11 at 20:57
  • Actually if you notice, OP has all the members of the class defined as `public static`. – anubhava Jun 17 '11 at 20:59
  • 1
    Yes, but `public` is not the issue here. The OP is asking about the difference between static and non-static. His/her members could all be `private`, and the issue would still be about comparing static and non-static. – Oliver Charlesworth Jun 17 '11 at 21:00
3

There are several reasons why this is a bad idea:

  1. Public field. Using public fields makes it practically impossible to write thread-safe code. It also makes it hard to maintain or modify your code. On a theoretical level it violates encapsulation, which is one of the basic ideas of OO with all its consequences. For example if you have complex state, where not every combination of field values is valid, you're in trouble.
  2. Static field. Although static fields have their legitimate uses, it should be kept to a minimum. They aren't inherited, which can easily lead to confusion and at the best of times it's a ticking time bomb.

All in all: don't use static fields unless it is necessary, and even then they should be private.

The notable exception is obviously static and final fields (aka. constants) which can be declared public without too many dangers.

biziclop
  • 48,926
  • 12
  • 77
  • 104
1

It depends entirely what you are doing. If your class just holds stateless utility functions then this may be OK. If you are trying to any kind of real OOP design, then this doesn't make sense.

If you use instances of classes to model objects in the 'real world', then they will need instance variables, and should have instance methods to act upon that data. Each instance encapsulates that data and provides suitable behaviour.

DNA
  • 42,007
  • 12
  • 107
  • 146
1

Generally speaking one never makes static variables except for static final variables which are then like constants. The primary reason is that more than one thread can then change the state of the global variable at the same time leading to unpredictable state of every object instance of that class.

Kirby
  • 15,127
  • 10
  • 89
  • 104
1

As per Object oriented fundamental concerns :

  1. Variable should not be accessible outside the class. So they should be private not public except interface case. This is applicable to Static variable as well.

  2. You want to access the variable use public method. In case of static variable you will static public method.

Kamahire
  • 2,149
  • 3
  • 21
  • 50