Types of Variables: Part 2
Enhance your knowledge about the different types of variables available in Java with more exciting examples in this lesson.
Parameters
We have seen different types of variables, and in some cases, we have seen a type of variable called parameters.
We need to remember one key thing about parameters. They are not classified as fields but classified as variables.
Coding example: 22
In the next two examples, we will see how parameters work. The signature for the main()
method in Java also uses parameters.
Press + to interact
/*there is exactly one copy of a class variable, regardless of how many timesthe class has been instantiated.*/class ExerciseSchedule {static int weightGain = 0;static int weightLoss = 0;public void eat(int weightGain){ExerciseSchedule exerciseOne = new ExerciseSchedule();this.weightGain = 2;System.out.println("After each meal the weight gain is : " + exerciseOne.weightGain);}public void run(int weightLoss){ExerciseSchedule exerciseTwo = new ExerciseSchedule();this.weightLoss = 2;System.out.println("After running two miles the weight gain is: " +( exerciseTwo.weightGain - exerciseTwo.weightLoss ));}}public class ExampleTwentyTwo {public static void main(String args[]){ExerciseSchedule newScheduleOne = new ExerciseSchedule();newScheduleOne.eat(2);newScheduleOne.run(2);ExerciseSchedule newScheduleTwo = new ExerciseSchedule();newScheduleTwo.eat(3);newScheduleTwo.run(3);}}
Coding example: 23
The following code snippet deals with the same example using parameters. Along with it, we have also used class variables with static
modifiers.
Press + to interact
class NewExerciseSchedule {static int weightGain = 5;static int weightLoss = 4;int rateOfPulse;public int eat(int weightGain){return weightGain;}public int run(int weightLoss){return weightLoss;}}public class ExampleTwentyThree {public static void main(String args[]){NewExerciseSchedule newScheduleOne = new NewExerciseSchedule();//first reference variablenewScheduleOne.rateOfPulse = 50;System.out.println("Total weight loss for the first object : " + (newScheduleOne.eat(2) -newScheduleOne.run(2))+ ". And the rate of pulse is : " + newScheduleOne.rateOfPulse);NewExerciseSchedule newScheduleTwo = new NewExerciseSchedule();//second reference variablenewScheduleTwo.rateOfPulse = 60;System.out.println("Total weight loss for the second object : " + (newScheduleTwo.eat(3) -newScheduleTwo.run(1)) + ". And the rate of pulse is : " + newScheduleTwo.rateOfPulse);//first reference variable points to the second object value and takes the new valuenewScheduleOne.rateOfPulse = newScheduleTwo.rateOfPulse;System.out.println("Total weight loss for the first object : " + (newScheduleOne.eat(10) -newScheduleOne.run(2))+ ". And the rate of pulse is : " + newScheduleOne.rateOfPulse);//since a class variable has been declared with a static modifier//we can access it with a class name//we need not instantiateNewExerciseSchedule.weightLoss = 1;System.out.println(NewExerciseSchedule.weightGain - NewExerciseSchedule.weightLoss);}}
Coding
...Access this course and 1400+ top-rated courses and projects.