What is Difference between class variable and instance variable with example?
Well, at the basic level, class fields, or static fields, as they are called sometimes, belong to the class and not to any particular instance of the class. Class fields can be assigned without instantiation of instance objects of a class. For example:
public class ExampleClass {
public static String classField = "";
public String instanceField = "";
}
I can assign a value to classField just like that:
ExampleClass.classField = "new value";
Instance fields, on the other hand, belong to a particular instance of the class. Thus, to assign an instance field a value, you have to first instantiate an object of the class just like that:
ExampleClass obj1 = new ExampleClass();
obj1.instanceField = "new value";
public class ExampleClass {
public static String classField = "";
public String instanceField = "";
}
I can assign a value to classField just like that:
ExampleClass.classField = "new value";
Instance fields, on the other hand, belong to a particular instance of the class. Thus, to assign an instance field a value, you have to first instantiate an object of the class just like that:
ExampleClass obj1 = new ExampleClass();
obj1.instanceField = "new value";
What is the difference between static global and local global variable?
From Java point of view, static global class members are shared among all class instances and can be accessed from any class member [Methods]. While local global [attributes] can be accessed from any class member [method] but not shared with other class instances Answer
The global variable can be shared across multiple files. But the global static variable cannot be shared across multiple files. Hence if you want the variable should be modified in some other file, you can make it a global variable and if you want a global variable only in the file you are declaring and not to shared across, you make it static global.the following code snippet should be useful. //ext2.c int a=10; static int s_b=89;
//ext.c extern int a; extern int s_b; int main() { printf("a = %d\n",a); printf("static b = %d\n",s_b); return 0; }
then build the exe with ext.o and ext2.o and try running.. you will encounter an error like this "./ext.o(.text+0x20): undefined reference to `s_b'"
What is the difference between declaring static variable as local and global?
Answer
There are two ways to declare varibles.1. Locally
2. Globally
When you declare a variable locally in any function that means it is only accessible by that function.
When you declare a variable globally so it is accessible by all the functions in the program.
Declaring variables with static keyword means you are setting its value null.
Difference between global variables and local variables?
Global Variable
It is a variable which is declared outside all the functions
It is a variable which is declared outside all the functions
It is a variable which is declared with in a function or with in a compound statement
Local Variable
It is accessible throughout the program
It is accessible only within a function/compound statement in which it is declared
No comments:
Post a Comment