Reputation: 17008
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
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
Reputation: 2209
As per Object oriented fundamental concerns :
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.
You want to access the variable use public method. In case of static variable you will static public method.
Upvotes: 1
Reputation: 49804
There are several reasons why this is a bad idea:
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
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
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
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