Tag Archives: Local

Global and Local Variables

A scope is a region of the program and broadly speaking there are three places, where variables can be declared:

  • Inside a function or a block which is called local variables,
  • In the definition of function parameters which is called formal parameters.
  • Outside of all functions which is called global variables.

We will learn what is a function and it’s parameter in subsequent chapters. Here let us explain what are local and global variables.

#include <iostream>
using namespace std;

int main ()
{
  // Local variable declaration:
  int a, b;
  int c;

  // actual initialization
  a = 10;
  b = 20;
  c = a + b;

  cout << c;

  return 0;
}

Global Variables:

Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program.

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:

#include <iostream>
using namespace std;

// Global variable declaration:
int g;

int main ()
{
  // Local variable declaration:
  int a, b;

  // actual initialization
  a = 10;
  b = 20;
  g = a + b;

  cout << g;

  return 0;
}

A program can have same name for local and global variables but value of local variable inside a function will take preference. For example:

#include <iostream>
using namespace std;

// Global variable declaration:
int g = 20;

int main ()
{
  // Local variable declaration:
  int g = 10;

  cout << g;

  return 0;
}

When the above code is compiled and executed, it produces the following result:

10

Initializing Local and Global Variables:

When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows:

Data Type Initializer
int 0
char ‘\0’
float 0
double 0
pointer NULL

It is a good programming practice to initialize variables properly, otherwise sometimes program would produce unexpected result.

Local and Anonymous Inner Classes in Java

Local and Anonymous Inner Classes in Java

Local Inner Classes in Java

If an inner class has been defined within a code block (typically within the body of a method), then such an inner class is called a local inner class. A local inner class is not a member of the enclosing class and hence it can not have any access specifier. A local inner class will have access to all the members of the enclosing class and it’ll have access to the local final variables in the scope it’s defined.

Example: a typical local inner class

public class EnclosingClass{
	...
		public methodName()
		{
			//...definition of the local inner class
			class LocalInnerClass 
			{
				...

			}
		...
}

Anonymous Inner Classes in Java

If an inner class has been declared without a name and within a code block (typically within the body of a method) then such an inner class is called an anonymous inner class. Since such an inner class doesn’t have a name associated, so it’ll be accessible only at the point where it is defined.

Example: a very common usage of an anonymous inner class

public class GUI extends JFrame{
	…
		public void buildComponents(){
		…
		button1 = newJButton();
		…
		button1.addActionListener(new java.awt.event.ActionListener(){
		public void actionPerformed(java.awt.event.ActionEvent ae){
		…
		…
		}
		});
		…
		…
	}
}

Since, an anonymous inner class doesn’t have a name, so it can’t have a named constructor. But, it can have an instance initializer. Access rules for anonymous inner classes are same as that of local inner classes. Similar to a local inner class, an anonymous inner class can also not have any access specifier attached to it.

Are anonymous inner classes same as local inner classes?

Yeah…, an anonymous inner class is a local inner class in all means except that it doesn’t have a name and hence it can’t be used at any other point except at the point where it has been defined. The other difference is that it can’t have a named constructor again for the same reason that it doesn’t have a name.