/ Java

Programs to know Variables in Java Clearly

In the previous article names Variable in Java, I have clearly described the scope and usage of various variables in Java, The use of local, global and static variables occurs according to the needs of the program.

In this article, I just created a class called Croatia. It will provide us the details of Overall team’s performance as well as a single player’s performance. You will know the basics of how to choose the appropriate variable type in the program. I will start by defining the variable static and then using an instance and a local variable in demand.

I will also write the variable usage, problem and completely describe the program.

Okay now, let’s start by creating the class Croatia.

public class Croatia {



    //Variables for team

    static int fouls =0;

    static int corners =0;



//Variables for players

    static String position="";

    static int goal=0;

    static int assists =0;

    static int yellowCard =0;

    static int redCard =0;



    public static void main (String args[]){

        Croatia modris = new Croatia();

        Croatia vida = new Croatia();



        //now vida scores goal;

        vida.position="";

        vida.goal = goal+1;

        vida.fouls=fouls+1;



        //again modris scores goal;

        modris.goal = goal+1;

        modris.fouls=fouls+1;

        modris.goal=goal+1;

        modris.yellowCard=modris.yellowCard+1;

        modris.assists = modris.assists+1;



        int totalGoals = goal;

        int totalYellowCards = yellowCard;



        System.out.println("\n=============Team Overall Statistics==========");

        System.out.println("Total Goals: "+totalGoals);

        System.out.println("Fouls: "+fouls);

        System.out.println("Total Yellow Cards: "+totalYellowCards);



        System.out.println("\n=============Vida's Performance============");

        System.out.println("Vida's Perfomance :");

        System.out.println("Total Goals: "+vida.goal);

        System.out.println("Vida's Assist: "+vida.assists);

        System.out.println("Total Yellow Cards: "+vida.yellowCard);



        System.out.println("==============Modris Performance============");

        System.out.println("Madris's Perfomance :");

        System.out.println("Total Goals: "+modris.goal);

        System.out.println("Madris Assist: "+modris.assists);

        System.out.println("Total Yellow Cards: "+modris.yellowCard);

    }

}

Final Output:

=============Team Overall Statistics==========================

Total Goals: 3

Fouls: 2

Total Yellow Cards: 1

=============Vida’s Performance=========================

Vida’s Perfomance :

Total Goals: 3

Vida’s Assist: 1

Total Yellow Cards: 1

==============Modris Performance======================

Madris’s Perfomance :

Total Goals: 3

Madris Assist: 1

Total Yellow Cards: 1

Description of Program

We started the program by declaring each of the variables as static. This means the variables for evaluating team’s performance and player’s performance are static. So every instance of the object shares the same memory place to store value.

So, the problem would arise when the individual player (object) is assigned the same goal as the team’s overall goal. Likewise, other variables also face the same problem.

Problem

In our program, Modric scores 2 goals while Vida scores 1 goal, but when we see the output, total goals scored by each player is 3. It is because of the static goal variable when the goal has scored the update is set in the single location and when the object accesses the variable, they refer to the same location.

Solution:

So in order to solve this problem, I separated the possible static variables and instance variable on the basis of the overall team’s performance and individual performance. So, the program is redesigned as:

public class Croatia {



    //These are the static variables that are shared by the Class instance. (Team)

    static int fouls =0;

    static int corners =0;





    //These are the instance variable shared by every object (players) of the class (Croatia)

    int goal=0;

    int assists =0;

    int yellowCard =0;

    int redCard =0;



    public static void main (String args[]){



        Croatia modris = new Croatia();

        Croatia vida = new Croatia();



        //now vida scores goal;

        vida.goal = vida.goal+1;

        vida.fouls=fouls+1;



        //again modris scores goal;

        modris.goal = modris.goal+1;

        modris.fouls=fouls+1;

        modris.goal = modris.goal+1;

        modris.yellowCard=modris.yellowCard+1;

        modris.assists = modris.assists+1;



        int totalGoals = vida.goal+modris.goal;

        int totalYellowCards = vida.yellowCard+modris.yellowCard;



        System.out.println("\n=============Team Overall Statistics===========");

        System.out.println("Total Goals: "+totalGoals);

        System.out.println("Fouls: "+fouls);

        System.out.println("Total Yellow Cards: "+totalYellowCards);



        System.out.println("\n=============Vida's Performance=============");

        System.out.println("Vida's Perfomance :");

        System.out.println("Total Goals: "+vida.goal);

        System.out.println("Vida's Assist: "+vida.assists);

        System.out.println("Total Yellow Cards: "+vida.yellowCard);



        System.out.println("==============Modris Performance============");

        System.out.println("Madris's Perfomance :");

        System.out.println("Total Goals: "+modris.goal);

        System.out.println("Madris Assist: "+modris.assists);

        System.out.println("Total Yellow Cards: "+modris.yellowCard);

   }

}

Output:

=============Team Overall Statistics==========================

Total Goals: 3

Fouls: 2

Total Yellow Cards: 1

=============Vida’s Performance=========================

Vida’s Perfomance :

Total Goals: 1

Vida’s Assist: 0

Total Yellow Cards: 0

==============Modris Performance======================

Madris’s Perfomance :

Number of Total Goals: 2

Madris Assist: 1

Total Yellow Cards: 1

Stat Inside Method

Description of the Program

In this program, we reorganize the variables that are likely to be used in the program. Instance variables like goal and assists will now have the different location for the objects (players). so, the individual scores and performance can be stored in their respective location. The general method of calling their own location is:

instance.variable_name

i.e If modris scores goal it will be modris.goal = modris.goal+1;

So, our problem of static and instance variable is solved.

Problem

Now, all the matter is solved, we must rewrite player’s performance every time the new player is added, this will result in a long and ineffective program.

Solution:

So we must create a method that will handle this so, that we can just call the method and method would function it for us. We can save enough time for the program. We must introduce a local variable and call it.

Let’s program our final program with the use of local variable along with the static and instance variable.

public class Croatia {



    //These are the static variables that are shared by the Class instance. (Class)

    static int fouls =0;

    static int corners =0;



    //These are the instance variable shared by every object (players) of the class (Croatia)

    int goal=0;

    int assists =0;

    int yellowCard =0;

    int redCard =0;



    public void stats(){

        //These are the local variables that are inside method, the same name has been given as to show the scope.

        int goal     = this.goal;

        int assists  = this.assists;

        int yellowCard = this.yellowCard;

        int redCard    = this.redCard;



        System.out.println("Total Goals: "+this.goal);

        System.out.println("Total Assist: "+this.assists);

        System.out.println("Total Yellow Cards: "+this.yellowCard);

        System.out.println("Total Red Cards: "+this.redCard);

    }



    public static void main (String args[]){



        Croatia modris = new Croatia();

        Croatia vida = new Croatia();



        //now vida scores goal;

        vida.goal = vida.goal+1;

        vida.fouls=fouls+1;

       

        //team gets corners

        corners=corners+1;



        //again madris scores goal;

        modris.goal = modris.goal+1;

        modris.fouls=fouls+1;

        modris.goal = modris.goal+1;

        modris.yellowCard=modris.yellowCard+1;

        modris.assists = modris.assists+1;



        int totalGoals = vida.goal+modris.goal;

        int totalYellowCards = vida.yellowCard+modris.yellowCard;

        int totalRedCards   = vida.redCard+modris.redCard;



        System.out.println("\n=============Team Overall Statistics===========");

        System.out.println("Total Goals: "+totalGoals);

        System.out.println("Total Fouls: "+fouls);

        System.out.println("Total Corners: "+corners);

        System.out.println("Total Yellow Cards: "+totalYellowCards);

        System.out.println("Total Red Cards: "+totalRedCards);



        System.out.println("\n=============Vida's Performance=============");

        vida.stats();



        System.out.println("==============Modris Performance=============");

        modris.stats();



    }



}

Output:

=============Team Overall Statistics==========================

Sum Total Goals: 3

Overall Fouls: 2

Total Corners: 1

Total Yellow Cards: 1

Total Red Cards: 0

=============Vida’s Performance=========================

Total Goals: 1

Total Assist: 0

Total Yellow Cards: 0

Total Red Cards: 0

==============Modris Performance======================

Total Goals: 2

Total Assist: 1

Total Yellow Cards: 1

Total Red Cards: 0

 

Description of the Program illustrating use of variables

Finally, we have created a fully functional program for the Croatia team where the team performance, as well as the player’s individual performance, is shown. I made the new method name stats(), which certainly shows the player’s performance. There is one new thing there called this which refers to the same class that calls it. So whenever Vida calls it, the stats will show from the memory location that has Vida’s value and prints it.

The stats() methods have also the same variable as an instance variable. It is ok to name because it has local scope while the instance variable has a global scope that doesn’t conflict with each other.

Conclusion

Finally, I have programmed this as to show the basic use of variables types in different ways. I hope you have learned something interesting about the selection of the variable and likely to use it accordingly in your program.

 

Download this article.

Furthermore,

Difference between C++ and Java

12 Buzzwords that make Java unique.

Dipendra Laxmi Bahadur Chand Thakuri
CEO @ Vine Software Innovation Company (Pvt.) Ltd. (VSIC)
View More

Comments:

1 comment(s)!
Types of variables in java with example - Saral Notes
[…] Program of static variable […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

back to top button
error: Please be respectful of copyright. Unauthorized use is prohibited.
%d bloggers like this: