Advertisement

const in a class? Possible?

Started by March 06, 2000 10:30 PM
3 comments, last by kkuza 24 years, 7 months ago
Recently I started learning Java and have fallen in love with it. In the process I have started to convert many of my C++ classes to JAVA standards and ran into a problem: Is a const variable possible in a class (I know of const member functions)? The compiler gives me a error on the following: class SCREEN { private: // CONST used const short VID640x480x32; const short VID800x600x32; const short VID1024x768x32; const short VID640x480x24; const short VID800x600x24; const short VID1024x768x24; SCREEN (); // };
The equivilant in Java is final static members.
i.e.

class AnObject {
public final static int CONSTANT_1 = 1;
}
Advertisement
I *think* const is allowed in Java, but you need to specify the value in the class declaration. So though this would give you an error:
const short VID640x480x32;

...this should work:
const short VID640x480x32 = 1;


~CGameProgrammer( );

~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
At least in Java 1.0, const was a reserved keyword, but not implemented in the language. This might have changed since then, but I''m pretty sure it hasn''t, seeing as the constants in Math are still declared as public static final.
const and static mean two different things in class members.

static: there is one variable for all instances of the class. By default, it''s mutable (i.e. can be changed).

const: variable cannot be changed after member initialization list is processed.

static const: there''s one unchangeable instance for all objects of the class.

This topic is closed to new replies.

Advertisement