Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Defining attributes of objects

Write down and run this program on your computer | Write down and run this program on your computer | Introduction to objects | The first program | Types. Primitive types. | Types of variables. Declarations. | More on operators and expressions | Numeric promotions | Objects and references | The class String |


Читайте также:
  1. Aristarchic attributes
  2. C) Defining the Operational Level Agreements for the technical teams
  3. Chapter 2. Defining Love and Its Permutations
  4. Classes and objects
  5. Defining activities.
  6. DEFINING CHARACTERS

Fields of a class specify components of objects of this class.
For example, objects representing pairs of integers consist of two integer numbers.

It must be noted in the definition of the class Pair. A natural way of doing it is to declare variables of adequate types.

public class Pair {

int a;
int b;

// definitions of constructors and methods...

}

Such notation specifies, that each object of the class Pair contains two integer numbers - it consists of two elements which are integers. The identifiers a and b are arbitrary names necessary to access these numbers.

Fields of classes are usually declared with the modifier private, which means that they can be accessed only from within the class in which they are defined. Such fields are not available from methods of other classes.

Defining fields of a class

[ public ] class AClassName {

[ field_modifier ] type_namevariable_name [ initializer ];
//....

}
Note:

 

Example:

public class Pair {
private int a;
private int b;
//...
}

Fields of a class may be objects (variables specifying objects - references). Consider the definition of a class Book describing books.

public class Book {
private String author;
private String title;
private double price;
//....
}

The variables of type String are references. They denote adequate objects - strings.

The definition of the class Pair states that each its object contains two elements - integers. After an object has been created and initialized it contains two elements - integer numbers with some values. A different object of this class contains some different numbers, with (possibly) different values than the first.

What happens, if the values of attributes are set neither by a constructor nor in any other way?
They have default values.

By default, during object creation, class fields are set to the value of ZERO:

 

Therefore, objects of the class Person:

class Person {
private String name;
private int age;
private boolean isEmployee;
}

just after creation have the following values of attributes: name == null, age == 0, isEmployee == false.

 


Дата добавления: 2015-11-16; просмотров: 78 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Useful examples| Defining operations on objects (methods)

mybiblioteka.su - 2015-2024 год. (0.005 сек.)