answersLogoWhite

0


Best Answer

It really depends on the programming language, but in general, this is true. In Java, for example, the scope of a variable declared in a method is the method - outside of the method it is inaccessible, and once the method finishes execution, the variable disappears.

User Avatar

Wiki User

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

Wiki User

11y ago

Yes, that's the way it usually works in most programming languages.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When a variable is declared within a method it ceases to exist when the method ends true or false?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

In most languages where does a local variables scope begin and end?

A local variable only exists within the scope in which it is declared. As soon as the scope ends, the variable ceases to exist. { // beginning of a scope, i does not yet exist int i = 42; // local variable declared, i now exists } // end of scope, i no longer exists


What is the scope of any variable?

The Scope of a variable defines the areas of a program where this variable would be visible and can be used. For ex: a. Method variables - are visible only inside the method where they are declared and hence their scope is only the method b. Class variables - are visible inside the class and can be used by any method inside the class and hence their scope is the whole class.


What is global variable in java?

A local class is declared locally within a block of Java code.You can even have classes declared inside a method. such classes are called local classes.


Differences between scope and visibility of a variable?

Scope of a variable is the lines of code from which it can be seen and/or manipulated. Scope can be thought of as visibility, because a variable's scope is where it is visible from. A variable declared within a function (or any block, for that matter) has scope only within that block. It is visible within that block, and from within any contained block, but not from within any containing (outer) blocks. However, it should be noted that if a variable's name is reused in a nested declaration, then the outer variable loses scope within that block. As a result, you can reuse, for instance, the variable "i" within a new block without compromising any use outside that block.


How do you access global variable from within the main function when the local variable is of the same name?

When you acess a global variable inside main function you must use the same name, because the variable declared as global can be accessed by any function/procedure on the class where it was defined.

Related questions

When a variable is declared within a method it ceases to exist when the method ends.?

Yes, that's the way it usually works in most programming languages.


What is object in methods in java?

I guess an object in a method would be any object variable, used within a method. An object variable of course is a variable that is declared as a variable of a certain class type.


In most languages where does a local variables scope begin and end?

A local variable only exists within the scope in which it is declared. As soon as the scope ends, the variable ceases to exist. { // beginning of a scope, i does not yet exist int i = 42; // local variable declared, i now exists } // end of scope, i no longer exists


What is the scope of any variable?

The Scope of a variable defines the areas of a program where this variable would be visible and can be used. For ex: a. Method variables - are visible only inside the method where they are declared and hence their scope is only the method b. Class variables - are visible inside the class and can be used by any method inside the class and hence their scope is the whole class.


What is global variable in java?

A local class is declared locally within a block of Java code.You can even have classes declared inside a method. such classes are called local classes.


What happen if you declare a global variable as static in C language?

When declared as static, the variable has internal linkage and its scope is restricted to the *.c file in which it is declared. It becomes visible to all functions within the file where it is declared and not to functions in other files.


Why can't a global variable can be viewed only within the module in which its declared?

Because that's what global means.


Differences between scope and visibility of a variable?

Scope of a variable is the lines of code from which it can be seen and/or manipulated. Scope can be thought of as visibility, because a variable's scope is where it is visible from. A variable declared within a function (or any block, for that matter) has scope only within that block. It is visible within that block, and from within any contained block, but not from within any containing (outer) blocks. However, it should be noted that if a variable's name is reused in a nested declaration, then the outer variable loses scope within that block. As a result, you can reuse, for instance, the variable "i" within a new block without compromising any use outside that block.


Can this pointer be used within the concept of the static method?

No. When a method is declared static, it is defined outside of any individual class reference.


How do you access global variable from within the main function when the local variable is of the same name?

When you acess a global variable inside main function you must use the same name, because the variable declared as global can be accessed by any function/procedure on the class where it was defined.


What is Difference between local variable and data members or instance variable?

A data member belongs to an object of a class whereas local variable belongs to its current scope. A local variable is declared within the body of a function and can be used only from the point at which it is declared to the immediately following closing brace. A data member is declared in a class definition, but not in the body of any of the class member functions. Data members are accessible to all member function of the class.


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.