answersLogoWhite

0


Best Answer

To be able to access a variable declared in one file/class from another file/class you need to declare that variable as "public".

Doing this is a very bad coding practice because exposing a variable of a class to be available publicly to other classes is totally against the Java object oriented concepts of Encapsulation and Data hiding. So, preferably you shouldn't be doing this.

However, a better way to do this is, declare the variable as private and have public accessor methods through which you can access this variable. This will ensure that your data is protected even if it is available outside the class

User Avatar

Wiki User

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

Wiki User

12y ago

You declare it extern in the file you want to access it from. In the file that it is declared, declaring it at file scope automatically makes it visible to all files in the linkage scope.

file1.c

extern int a;

...

... use a

file2.c

int a

...

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

extern

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which is the keyword used to access the variable declared as global variable in another file?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Describe about storage allocation and scope of global extern static local and register variables?

Global Varible: The variable which is declared as "Global" one : having the preveleges to use and access that variable in any class and object( means any where in the program) just like PUBLIC keyword in OOPS concepts. Static Variable : If we declare a variable as Static , then it wont have the permission to access that variable through out the program and u have to use it inside the class or object which u declared itself. All the Best Annapurna


what type of variable is visible to every module and the entire program?

If you are talking about a class in Java, a variable encapsulated by a class is called an instance variable b/c everytime you create an object with that class, each object has its own set of the variables declared.


Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable. Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.


What type of reference is C19?

A reference variable is used to refer to or give access to an object. A reference variable is declared to be of a particular type and that type can not be altered.


Why a static member method can access only static members in java?

Because, the keyword static signifies the fact that the method or variable that is qualified using the static keyword is not attached to any object of the class. Therefore we cannot instantiate the class and use the object to reference to access it. The only option we have is to use the class name to directly access them

Related questions

What is the key word used to access the variable declared as global variable in another file?

r+ is the keyword


What does In public mean?

The keyword public is an access specifier. A variable or a method that is declared public is publicly accessible to any member of the project. Any class or method can freely access other public methods and variables of another class.


What is an access specifier?

An access specifier is a keyword applied to a variable, or method, which indicates which parts of the program are permitted to access it.


What is volatile thread?

Threads are not volatile. The volatile is a keyword that is used while having multiple threads in a Java Program. This keyword is used when multiple threads access a particular variable but still it is not mandatory to synchronize or prevent access to that variable.


What is the difference between access specifier and access modifier?

An access modifier is another name for an access specifier, which in object-orientated software is a keyword applied to a variable which indicates which other parts of the programme are permitted to access it.


Describe about storage allocation and scope of global extern static local and register variables?

Global Varible: The variable which is declared as "Global" one : having the preveleges to use and access that variable in any class and object( means any where in the program) just like PUBLIC keyword in OOPS concepts. Static Variable : If we declare a variable as Static , then it wont have the permission to access that variable through out the program and u have to use it inside the class or object which u declared itself. All the Best Annapurna


Can variable declared in one scriplet access in another scriplet?

No. Variables declared inside a scriptlet are like method local variables which are not accessible outside the scriptlet/method.


what type of variable is visible to every module and the entire program?

If you are talking about a class in Java, a variable encapsulated by a class is called an instance variable b/c everytime you create an object with that class, each object has its own set of the variables declared.


Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable. Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.


What type of reference is C19?

A reference variable is used to refer to or give access to an object. A reference variable is declared to be of a particular type and that type can not be altered.


Why a static member method can access only static members in java?

Because, the keyword static signifies the fact that the method or variable that is qualified using the static keyword is not attached to any object of the class. Therefore we cannot instantiate the class and use the object to reference to access it. The only option we have is to use the class name to directly access them