Net-informations.com
SiteMap  | About    

Difference between static and constant

What is a variable ?

A variable, its value can be changed by the program at runtime. The accessibility or the scope of a variable refers to where the variable can be read from or written to, and its lifetime, or how long it stays in the computer memory.

e.g.

int i;

Constant

A Constant is something that will always remain the same though out the entire lifetime of a program. A Constant variable cannot be modified after it defines and it cannot be change throughout the program. The Constant with a fixed value tells the compiler to prevent the programmer from modifying it. Whenever you try to change it, it will throw an error message. Constant variables declares with const keyword and can be used with primitive data types . Constants are set at compile time itself and assigned for value types only.

e.g.

public const double PI = 3.14159;

Static

Static variable is a property of a Class rather than the instance of class. It is stored on the data segment area of memory and the same value is get shared to all instances of that class. It can be assigned for reference types and set at run time. The static modifier can be applied with classes, fields, methods, properties, operators, events and constructors. It represent a kind of a global value for all the instances of that class and can able to call them using class name.

Product.price = 100;

Where Product is class name and price is a static declaration.







net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.