It's not a secret anymore!
The moose likes Beginning Java and the fly likes about global,local,class variable..!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about global,local,class variable..!!!" Watch "about global,local,class variable..!!!" New topic

about global,local,class variable..!!!

sharma anurag

Joined: Aug 29, 2007
Posts: 16
hi to all my friends of this is Forum,

please tell me about global,local and class variable,with thier respective examples.

Thanks in advance.
Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Hi sharma,
Global variables are those which are not declared in the methods.Scope of these variables are inside that class only.

Local variables are those which are declared in the methods.Scope of these variables are only inside that methods.


class sharma{

int a;---------->Global Variables

void call(){

int b;----------->Local variables


I hope you got it !!!

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

A global variable is a variable available in every scope. Java doesn't really support global variables (though a public static variable is as close as you'll get).

class variables are variables defined once for the class, i.e. :

Because they are static they are associated with the class rather than a particular object instance (i.e. every instance of Foo shares the same bar variable).

A local variable is a variable available in a limited scope. It differs from an instance variable, which is available to an instance of an object, because it is further limited by the scope of a block of code. Examples:

Make sense?
[ August 30, 2007: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: about global,local,class variable..!!!
jQuery in Action, 3rd edition