Programming keyword that specifies that a object or variable is not modifiable

Asked By 0 points N/A Posted on -
qa-featured

I am having hard time figuring the right keyword, what is the keyword that specifies that a object or variable is not modifiable after it is initialized?

SHARE
Best Answer by Felipa Dolore
Answered By 0 points N/A #177853

Programming keyword that specifies that a object or variable is not modifiable

qa-featured

The right keyword is "const". A variable declared as a "const" type can't be modified after it's initialized. If you try to update its value somewhere in your code there will be a compilation error.

If you are having hard time to figure out such keywords, you are most probably novice to C++ programming, so you better check out this tutorial here.

After completing the tutorial you are welcome to ask further questions.

Avila Hoosier

Best Answer
Best Answer
Answered By 20 points N/A #177854

Programming keyword that specifies that a object or variable is not modifiable

qa-featured

The answer to your question will largely depend on the programming language that you are using. A keyword that specifies that a variable is not modifiable after it is initialized can vary from one language to another. Like if you are using Java, "final" is the keyword used. See sample code below:

public final class ShapeClass {
     int shapeSides = 3;
 
     final void setShape() {
          shapeSides = 4;
     }
 
     public static void main(String [] args) {
          ShapeClass c1 = new ShapeClass();
          c1.setShape();
     }
}

It can be used in different circumstances to define an entity which cannot be changed at a later time.

Related Questions