answersLogoWhite

0


Best Answer

class Demo

{

static int a=10;

}

class Demo1

{

public static void main(String[] args)

{

Demo d1=new Demo();

Demo d2=new Demo();

d1.a=20;

d2.a=30;//this is the final value stored in that static variable

}

}

Finally what i want to say is whenever we modify static variable by two objects the value stored in that variable is the value given by last object.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What happens when two objects try to modify static variable in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are static objects in java?

There is no separate entity as a static object in java. The static keyword in java is used to signify that the member (either a variable or a method) is not associated to an object instance of the class. It signifies the fact that the member belongs to the class as a whole. The words static and objects are opposites of one another so you cannot have a static object. However, you can declare an object as a class level variable which could be referred to as a static object but it will be referred to as a static or class variable and not a static object.


What is the advantage of static character variable?

Static variables exist for the entire duration of the program. Therefore a static character variable is a character that exists for the entire duration the program is running. Regardless of where a static variable is declared and initialised, it is physically instantiated and initialised when the program loads. If the variable has function scope, whatever value is currently assigned to the variable will remain in effect the next time that function is called, but only the function can modify the variable. If the variable has class scope, then the variable exists even if no instances of the class have yet been instantiated, but any and all instances share the variable (it is common to the class, not to any one instance of the class). Static class variables must be initialised outwith the class body and outwith any other function body, but can only be modified by the class members (both static and non-static) and by friends of the class. If the variable has file scope, then the variable is a global variable. Global variables are largely frowned upon as there's no way to control what code can modify the value. Passing local function variables to other functions is the preferred method as this gives much greater control over which code can access and/or modify the value, as well as limiting the lifetime of the variable. To ensure the variable exists throughout a program's lifetime (as per a static variable), instantiate the variable from within the program's entry point.


Is it legal in java to modify a variable in a static method?

It is, provided that the variable is also static or you're referencing the variable through an instance of you class. class MyClass { static int staticVariable; int memberVariable; public static void main(String[] args) { // This is legal. staticVariable = 10; // This is not legal. memberVariable = 10; // This is legal. MyClass mc = new MyClass(); mc.memberVariable = 10; } }


What is static variable in the class for c plus plus?

Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.


What facility static gives in class?

Static variables or function provides global view to the application that is u can access a static variable with one or more than one objects of a single class without loosing i.e. reinitializing its value.

Related questions

What are static objects in java?

There is no separate entity as a static object in java. The static keyword in java is used to signify that the member (either a variable or a method) is not associated to an object instance of the class. It signifies the fact that the member belongs to the class as a whole. The words static and objects are opposites of one another so you cannot have a static object. However, you can declare an object as a class level variable which could be referred to as a static object but it will be referred to as a static or class variable and not a static object.


What is importance of static variable?

The static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.


How do you access the static variable and static methods of a class?

In java we access static variables and static methods without creating objects. i.e.,we can access directly by using classname we can also access static variables and static methods by using objects which are created by using class where the static variables and static methods are available


What is the advantage of static character variable?

Static variables exist for the entire duration of the program. Therefore a static character variable is a character that exists for the entire duration the program is running. Regardless of where a static variable is declared and initialised, it is physically instantiated and initialised when the program loads. If the variable has function scope, whatever value is currently assigned to the variable will remain in effect the next time that function is called, but only the function can modify the variable. If the variable has class scope, then the variable exists even if no instances of the class have yet been instantiated, but any and all instances share the variable (it is common to the class, not to any one instance of the class). Static class variables must be initialised outwith the class body and outwith any other function body, but can only be modified by the class members (both static and non-static) and by friends of the class. If the variable has file scope, then the variable is a global variable. Global variables are largely frowned upon as there's no way to control what code can modify the value. Passing local function variables to other functions is the preferred method as this gives much greater control over which code can access and/or modify the value, as well as limiting the lifetime of the variable. To ensure the variable exists throughout a program's lifetime (as per a static variable), instantiate the variable from within the program's entry point.


Is it legal in java to modify a variable in a static method?

It is, provided that the variable is also static or you're referencing the variable through an instance of you class. class MyClass { static int staticVariable; int memberVariable; public static void main(String[] args) { // This is legal. staticVariable = 10; // This is not legal. memberVariable = 10; // This is legal. MyClass mc = new MyClass(); mc.memberVariable = 10; } }


What is static variable in the class for c plus plus?

Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.


what is the difference between final and static keywords?

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...


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


Scope of static variables?

Scope of static variable is with in the file if it is static global. Scope of static variable is with in the function if variable is declared local to a function. But the life time is throughout the program


What should you make static in Java?

Static keyword signals something that is shared among all objects of the same class: i.e. a variable that all objects of the class can reference to, or method that all objects of the class can access. Usually, class constants are made static since it is the same in all objects of the same class thus it saves space not having to have variables for the same data for each class. But usually private instances should not be made static since it creates some strange situations, i.e. if you modify the data of one object, the data of the other object of the same class is also changed. Statics methods are common in main classes, in fact the main method itself is static, meaning it does not need a object to call it. The helper methods in a main class is usually static, since main classes are non-instantiable.


What facility static gives in class?

Static variables or function provides global view to the application that is u can access a static variable with one or more than one objects of a single class without loosing i.e. reinitializing its value.


How do you create static variables in PHP?

Use the "static" keyword to declare a static variable within a function like shown below. <?php function fun() { static $variable; static $another_variable = ''; } ?>