Local variables

Compiled By Unknown - No Comments
 public class VariablesAndNames  
 {  
   public static void main( String[] args )  
   {  
     int cars, drivers, passengers, cars_not_driven, cars_driven;  
     double space_in_a_car, carpool_capacity, average_passengers_per_car;  
     cars = 100;  
     space_in_a_car = 4.0;  
     drivers = 30;  
     passengers = 90;  
     cars_not_driven = cars - drivers;  
     cars_driven = drivers;  
     carpool_capacity = cars_driven * space_in_a_car;  
     average_passengers_per_car = passengers / cars_driven;  
     System.out.println( "There are " + cars + " cars available." );  
     System.out.println( "There are only " + drivers + " drivers available." );  
     System.out.println( "There will be " + cars_not_driven + " empty cars today." );  
     System.out.println( "We can transport " + carpool_capacity + " people today." );  
     System.out.println( "We have " + passengers + " to carpool today." );  
     System.out.println( "We need to put about " + average_passengers_per_car + " in each car." );  
   }  
 }  

  • Local variables are declared in methods, constructors, or blocks. 
  •  Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block. 
  •  Access modifiers cannot be used for local variables. 
  •  Local variables are visible only within the declared method, constructor or block. 
  •  Local variables are implemented at stack level internally. 
  •  There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.
Example of local variables are
 public class Test{   
   public void pupAge(){  
    int age = 0;  
    age = age + 7;  
    System.out.println("Puppy age is : " + age);  
   }  
   public static void main(String args[]){  
    Test test = new Test();  
    test.pupAge();  
   }  
 }  


for more examples
 public class MoreVariablesAndPrinting  
 {  
   public static void main( String[] args )  
   {  
     String myName, myEyes, myTeeth, myHair;  
     int myAge, myHeight, myWeight;  
     myName = "Zed A. Shaw";  
     myAge = 35;   // not a lie  
     myHeight = 74; // inches  
     myWeight = 180; // lbs  
     myEyes = "Blue";  
     myTeeth = "White";  
     myHair = "Brown";  
     System.out.println( "Let's talk about " + myName + "." );  
     System.out.println( "He's " + myHeight + " inches tall." );  
     System.out.println( "He's " + myWeight + " pounds heavy." );  
     System.out.println( "Actually, that's not too heavy." );  
     System.out.println( "He's got " + myEyes + " eyes and " + myHair + " hair." );  
     System.out.println( "His teeth are usually " + myTeeth + " depending on the coffee." );  
     // This line is tricky; try to get it exactly right.  
     System.out.println( "If I add " + myAge + ", " + myHeight + ", and " + myWeight  
       + " I get " + (myAge + myHeight + myWeight) + "." );  
   }  
 }  

 package corejava;  
 public class Main {  
     public void Age() {  
         int age = 0;  
         age = age + 7;  
         System.out.println("Age: " + age);  
     }  
     public static void main(String args[]) {  
         Main m = new Main();  
         m.Age();  
     }  
 }  
You can print things out with System.out.println and you can do math. The next step is to learn about variables. In programming a variable is nothing more than a name for something so you can use the name rather than the something as you code. Programmers use these variable names to make their code read more like English, and because programmers have a lousy ability to remember things. If they didn't use good names for things in their software they'd get lost when they came back and tried to read their code again.

Tags:

No Comment to " Local variables "