Static variables and static methods in Java Assignment Help

Assignment Help: >> Information hiding >> Static variables and static methods in Java

Static variables and static methods in Java:

So far, we have talked about each object having its own data and methods, and we have invoked methods on individual objects. This is the normal situation, but there may be occasions when we want to share an item of data between objects. There is another type of variable called a static variable, also known as a class variable. When you declare a static variable in a class, you are specifying that there is exactly one copy of the variable for all objects defined by that class. The name 'class variable' reflects that the variable is created once for the whole class and not individually for the objects of that class. You may find it useful to think of it as a class-wide variable. Static variables and methods are both introduced by means of the keyword static, which is another example of a modifier. 

Let us add to our example a new static variable, totalAccesses, and two related methods:

public class User
{

private int numOfAccesses;

// static (class) variable declaration

private static int totalAccesses;

public void updateNumOfAccesses ()

{

numOfAccesses++;

}

public int getNumOfAccesses ()

{

return numOfAccesses;

}

public void updateTotalAccesses ()

{

totalAccesses++;

}

public int getTotalAccesses ()

{

return totalAccesses;

}

// and other methods

}

Notice that we added the word static after the word private, so that we provided further information about the kind of variable totalAccesses is. Consider the following test class:

public class TestUser
{

public static void main (String []args)

{

// this method has two User references

User uOne, uTwo;

// once initialized, the references can be used

// to access members of the User objects they

// reference

uOne = new User();

uTwo = new User();

uOne.updateNumOfAccesses();

uOne.updateTotalAccesses();

uTwo.updateTotalAccesses();

System.out.println(uOne.getNumOfAccesses());

System.out.println(uTwo.getNumOfAccesses());

System.out.println(uOne.getTotalAccesses());

System.out.println(uTwo.getTotalAccesses());

}

}

Bearing in mind that the integer instance variables in our User objects are initialized to zero by default, the output of the TestUser program would be:
1

0

2

2

This is because there is an instance of numOfAccesses for each object of the class User we make, so any changes to the value of this variable are made only in the object that makes them; however, there is only one copy of totalAccesses for all objects of the class, so any changes to this variable are seen in all objects of the class. 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Static variables and static methods in Java homework and assignments? Live Static variables and static methods in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Static variables and static methods in Java homework help, java assignment help and Static variables and static methods in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd