The Essence of OOP using Java: Static Members

2008-02-23 09:45:59来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

The Essence of OOP using Java: Static Members


Preface

This lesson is one of a series of lessons designed to teach you about the essence of Object-Oriented Programming (OOP) using Java. The first lesson in the group was entitled The Essence of OOP Using Java, Objects, and Encapsulation. That lesson, and each of the lessons following that one, has provided explanations of certain ASPects of the essence of Object-Oriented Programming using Java. The previous lesson was entitled The Essence of OOP using Java, Polymorphism and Interfaces, Part 2.

Necessary and significant aspects

This miniseries will describe and discuss the necessary and significant aspects of OOP using Java. If you have a general understanding of computer programming, you should be able to read and understand the lessons in this miniseries, even if you don't have a strong background in the Java programming language.

Supplementary material

I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at Baldwin's Java Programming Tutorials.

Preview

Static members

There is another aspect of OOP in Java that I have avoided up to this point in the discussion: static variables and static methods.

Tends to complicate ...

I have avoided this topic because, while not particularly difficult, the existence of static members tends to break up the simple structures that I have discussed in previous lessons in this miniseries.

While static members can be useful in some situations, the existence of static members tends to complicate the overall object-oriented structure of Java.

Avoid overuse of static members

Furthermore, the overuse of static members can lead to problems similar to those experienced in languages like C and C that support global variables and global functions.

When to use static members

I will discuss the use of static members in this lesson, and will provide some guidelines for their use.

The class named Class

I will also introduce the class named Class and discuss how it enters into the use of static variables and methods.

Instance members vs.. class members

I will describe the differences between instance members and class members with particular emphasis being placed on their Accessibility.

Three kinds of objects

From a conceptual viewpoint, there are at least three kinds of objects involved in a Java program:

  • Ordinary objects
  • Array objects
  • Class objects
Ordinary objects

All of the discussion up to this point in the miniseries deals with what I have referred to in the above list as ordinary objects.

These are the objects that you instantiate in you code by applying the new operator to a constructor for a class in order to create a new instance (object) of that class. (There are also a couple of other ways to create ordinary objects, but I'm not going to get into that at this time.)

Array objects

I haven't discussed array objects thus far in this miniseries. (I plan to discuss them in a subsequent lesson.)

Suffice it for now to say that Array objects are objects whose purpose is to encapsulate a one-dimensional array structure that can contain either primitive values, or references to other objects (including other Array objects).

I will discuss Class objects in this lesson.

Discussion and Sample Code

Class objects

Let me emphasize at the beginning that the following discussion is conceptual in nature. In this discussion, I will describe how the Java system behaves, not how it is implemented. In other words, however it is implemented, it behaves as though it is implemented as described below.

The class named Class

There is a class whose name is Class. The purpose of this class is to encapsulate information about some other class (actually, it can also be used to encapsulate information about primitive types as well as class types).

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Tomcat配置SQLServer连接池

下一篇:struts spring hibernate架构中数据对象(PO,POJO,FormBean)的