Ticker

6/recent/ticker-posts

Variables in JAVA | Local & Global Variables

Variables


Local Variables 

Any variables declared inside a method or constructor is called has local variables. Local variables need to be initialised before they are used. Local variables do not have default value. 

Global Variables 

Any variables declared inside the class but outside the methods or constructors are called has global variables. Global variables will take a default if u have not initialised it. 

Instance variables the global variables declared without static keyword is called has instance variables. These variables get initialised whenever the object is created. Instance variables are one for the object. 

Static variables the global variables declared with the static keyword are called has static variables. These variables will get initialized whenever the class loads into memory. These variables are one for the class. 

An argument is an expression passed to a function or constructor, where a parameter is a reference declared in a function declaration or even constructor.

class Gift 

        void buy(String name , int cost) //name and cost r called has formal parameters 

        

                //some code hear 

        

        void Gift(double d , char c) //d and c are called has formal parameters 

        

            //some code 

        

        public static void main(String[] args) 

        

                double e = 5.5; 

                char v = ‘M’; 

                Gift g = new Gift(e , v); //e and v are the actual arguments 

                String hesaru = "barby"; 

                int money = 200; 

                g.buy(hesaru , money); //hesaru and money are the actual arguments 

    }

}

ALSO SEARCH:

"local variables and global variables in java"

"what is global variable in java with example"

"what is local and global variable in java"

"what is global variable in java"

"how to have global variables in java"

"how to set global variables in java"

"what is local and global variables"

"local instance and global variables in java"