answersLogoWhite

0


Best Answer

Static:

we can use the keyword static either to method or to a variable.

when we declare to a method,(eg: public static void main(String args[]),we can use this method without any object.

when we use to a variable,there will be only one instance of that variable irrespective of how many objects that get created of that class.

Final:

Usage of final to method or to a variable makes them as constant. It's value cannot be changed...

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

final keyword when used with a variable makes it a constant which means we cannot change it's value further:

eg:

final int MY_CONST = 30; // creating a constant with final keyword

MY_CONST = 40;//this line will generate an error as MY_CONST is a constant

--------------------------------------

static variables are those which are associated with the class we need not to instantiate class in order to use them, we can directly call them using the class name.

eg:

class StaticClass

{

static int myvar = 30;

public static void main(String[] args){

System.out.println(StaticClass.myvar);//will print 30

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

In Java (or in any OOPL) static means, the member we are defining as static will act as a global member for all the objects. That means, suppose there is a "static int i" in a class ABC. The data i will be common to all of its objects.

In the case of a Class attribute (variable), static indicates that all instances of that class share that attribute's value. So, if one instance modifies that attribute, other instances will see their value for that attribute change.

In the case of Class methods, static makes that method fixed for all subclasses, and also makes it possible to call that method via a Class call, even if there are no instances of that Class instansiated. Static methods can only manipulate or access static attributes of a Class, never any instance's attributes. For instance, in class FOO there is a static method Me. Other objects could call FOO.Me() in their code, without there ever having been a FOO object created. A good example of this is the Math class, most of whose methods are static.

Final can also be applied to classes OR methods. In both cases, this means that there can be no more modifications of it. In the case of a final Class, that means there can be no subclasses. Final methods mean that no subclass can override this method.

Static Final used together on Class attribute are the way Java defines what other languages call constants.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

In C++, the const keyword has several uses depending on whether it applies to a primitive data type, an object or a member function. When prefixed to a primitive data type, that type's value remains constant; once assigned a value it can never be inadvertently changed by the programmer. The same applies to an object prefixed with the const keyword, however the object's mutable members remain mutable; only the immutable members are rendered constant. When an object is rendered constant, only constant member functions can be invoked upon the object. Similarly, when a constant member function is invoked, the object is rendered constant even if it was not constant prior to the method being invoked.

In Java, the final keyword does a similar job to the C++ const keyword with respect to primitive data types; it renders a value constant. However, when applied to objects, it differs greatly. In C++, we can use the const keyword to alter the constness of an object from both inside and outside of its class. But in Java, the constness of an object can only be done from inside the class. Declaring an object as final does not make it a constant object, it simply makes the reference constant. That is, we cannot refer to any other object other than the one we initially assigned to the reference (because it is constant), but we can still modify the object being referenced unless the class (or subclass) dictates otherwise.

The final keyword in Java can also be used to prevent subclassing of a class as well as to prevent specific methods from being overridden by subclasses. In this respect it has the same purpose as the final keyword in C++.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The two are quite unrelated. "static" means that only a single copy of a field is stored for a class, instead of one copy for each object, which is the case for non-static fields. (Note: A "field" is a variable declared at the class level, outside of any method.) Such a static field can be accessed from any object created as an instance of the class; or it can be accessed without creating any object at all (for example, Math.PI, in the standard classes, which simply stores the number pi).

A final variable is one that, after being created, can't be changed. In other words, a constant. The example above, Math.PI, also happens to be final; there is no need to change it. However, in general, fields can be created in any combination between static and nonstatic on the one hand, and final and non-final on the other.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Constant value is known at runtime whereas final value may not be known at runtime.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Final refers to happening at the end of something; finally means at long last while finalize means to put the last touches to something.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A static variable is shared among different copies (objects) of a class.

A final variable is one whose contents can't be changed, once a value is assigned - sort of like a constant.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between const in C and final in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is difference between constant in c plus plus and final in java?

The final keyword can be used to modify a class, method, or variable.When used on a variable, it means that variable cannot be changed once set.When used on a method, it means that no subclasses may override that method.


What is difference between java 2 and java 5?

They are different versions. Java 5 is newer than Java 2. Think of it like the difference between the Playstation 1 and the Playstation 3.


Major difference between c and java?

Java is object oriented, C is not...


What is the difference between connectivity in java?

kamina


What is difference between connectivity in java?

kamina


What is the main difference between UNIX and JAVA?

Unix is an operating system, Java is a language.


Difference between recordset and resultset in java?

Rowset


What is the difference between JAD and JAR?

JAD-Java Application Description JAR-Java archive


What is the difference between java and object oriented programming?

java is a programming language/platform that embodies object oriented programming concepts. The question of what is the difference is like asking what is the difference between cars and a Volvo.


What is the difference between c plus plus and java programming?

Java doesn't have pointers. C++ has pointers.


What difference between InputStreamReader and DatainputStream in java?

Both are same


WAP to show the difference between a variable and static variable?

difference between constant and static variables in java