English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How do we initialize the constant data members of a class when an object is initiated? Give an example.

2007-06-26 14:57:24 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

In Java, you'd use either a constructor or specified values in the declaration:

class MyClass {
int myValue1 = 3; // One way
int myValue2;

MyClass() {
myValue2 = 5; // Another way
}
}

2007-06-26 15:01:04 · answer #1 · answered by McFate 7 · 0 0

Constant data members in a class aren't initialized during instantiation of an object at all; they're initialized when the class is defined:

public class MyClass {
public static final int MY_CONSTANT = 10;
/* remainder of class definition */
}

2007-06-26 15:15:54 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers