JDS
JDS

Reputation: 17008

Java - about using classes with static members vs. not

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.

Upvotes: 2

Views: 4417

Answers (6)

DNA
DNA

Reputation: 42617

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.

Upvotes: 1

Kamahire
Kamahire

Reputation: 2209

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.

Upvotes: 1

biziclop
biziclop

Reputation: 49804

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.

Upvotes: 3

Kirby
Kirby

Reputation: 15875

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.

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272792

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.

Upvotes: 8

anubhava
anubhava

Reputation: 786261

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.

Upvotes: 3

Related Questions