开发者

Aren't fields in classes similar to global variables?

I started learning a functional programming language(SML) and programmed in this language a little bit. And then i went started checking Java and i had this feeling that class fields seem like global variable开发者_如何学Pythons and they complicate programming. For example i must read methods to see which one read/write them etc.

From what i have heard using global variables in programming languages like C is a bad idea. But what about Java class fields, aren't they something like global variables for all your class methods? Is it a bad idea to use fields? (Or maybe i have understand something wrong or i program in the "wrong way" Java)


I assume by "class variables" you mean "static variables".

You ask "aren't they something like global variables for all your class methods?".

Yes, you are right. Within the class the behave like globals with all their issues.

The difference is that your classes should not be as complex as whole programs and thus it would be easier to understand and fix the problems caused by them.
The less code can modify a variable the less cases you have to consider. Globals can be modified by arbitrary unknown code.

It also makes absolute sense in some situations to have all instances of a class share a variable (e.g. a singleton). You just have to use it responsibly.

Should you not use them?
No, you can use them. But limit their visibility to the needed minimum so they don't become 'de facto' globals.
Also make them final if possible.


Class level variables are global variables in the context of the class. That is done to keep some state, you need to have them somewhere. In some cases, Class level variables are considered bad practice though especially when they are not immutable.


I would say that you don't quite understand the way Java (or any other object oriented language) works.

In object oriented languages, classes represent different types of things you may need in your whole program. It's better to ilustarte this with an example. Let's say in your program you are going to model cars.

You would have a Car class, and would create a new object (a new instance) of the Car class to represent each new car you need. The car itself is constituted by different components, you have wheels, a motor, windows, etc. So you would have classes for each of these components, and each car object would contain its own set of objects from all the different classes, ie:

Car1 {
       motor1
       window1.1, window1.2
       wheel1.1,wheel1.2
}

Car2 {
       motor2
       window2.1, window2.2
       wheel2.1,wheel2.2
}

In this case, you would define each of the cars' components as class fields. These fields are for all effects 'global' in that they can be accessed from any of the methods in that class. The detail you seem to be missing is that each new object of this class has its own set of fields and methods. They don't share them, so each motor, set of wheels, etc., belongs to one instance of the Car class. So if in the car class you have a method called, lets say, deleteWindows(), which would get rid of all the windows, and you called that method on car2, this would not delete car1's windows.

Another important detail is that you can define these variables with several 'prefixes' (methods too). First you have public and private (protected to, but I won't get into that). By declaring a variable as private, you are saying the only object who can access and change that variable is the one who owns it. A public variable on the other hand can be accessed and changed by any other object. They are accessible, but you have to explicitly say you want to change that object's variable (by writing objectsName.variable, in our case car1.motor).

You can also have variables/methods that are shared by all instances of a class. To do this you declare them as static (these effectively belong to the class and not to any object of tha class in particular). Private/public still apply, a private static variable is only accessible by instances of that class (and static methods of the same class), while public ones are accessible by any other class/object. In order to access them from outside the class they belong to, you use ClassName.variable/method (so Car.variable in the previous example).

Sometimes you might want to have a class that would make no sense creating an instance of. I find I often need to create a Maths class which contains several mathematical operator that I want to use throughout the whole program. It makes no sense creating a Maths object, so you would just define all its methods as 'public static' and access them whenever you need in your other classes.

I hope I have clreaded your doubt. Either way I would suggest you do some reading on object oriented programming, maybe get a book that teaches Java with a heavy initial focus on object orientation (OO), as even though it isn't a hard concept to grasp, it might be hard getting used to it and correctly programming in an OO language if you come from a non-OO background.

You might want to look into BlueJ. It's a Java IDE which basically forces you to understand and use OO programming. It's not something I suggest using for too long, but it might be a good place to start until you get a good grasp on the OO basics.

Zepee


No, class fields are not global variables, though they could be misused for much the same purpose.

Global variables are accessible from every scope. They tend to be writable from every scope as well. This makes large codebases difficult to understand and debug. Global variables also invite nameclash.

Class fields are in class scope. They tend to be encapsulated in a class, with private access, preventing access from outside the class. This limits direct access and modification to a small subset of code.


But In Java whole program is not written with using a single Class. And it is constants(public static final fields) that are like global variables. And my advice is for you not concentrate on each of this single part of the java, what good about the java is the what it deliver as a whole. Because that's when you see the impartance of each of this features of java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜