Accessing Static Methods and Variables

Accessing Static Methods and Variables

Accessing Static method is very impotent for the any code .When instance of the classes in the code is absence then how to code is appropriate that  we can use the accessing static method for make a appropriate code. 
So in this way we access a static method (or static variable) is to use the dot operator on the class name, as opposed to on a reference to an instance, as follows :
class Frog {
static int frogCount = 0; // Declare and initialize static variable
public Frog() {
frogCount += 1; // Modify the value in the constructor

class TestFrog {
public static void main (String [] args) {
new Frog();
new Frog();
new Frog();
System.out.print(\"frogCount:\"+Frog.frogCount); //Access static variable

Another point to remember is that static methods can\'t be overridden! This doesn\'t mean they can\'t be redefined in a subclass, as we\'ll see a little later when we look at overriding in more detail, but redefining and overriding aren\'t the same thing.

Things you can mark as static:
Top-level nested classes
                  Things we can\'t mark as static :
Inner classes
Inner class methods and instance variables
Local variables

Date:2025-02-09 00:00:00

Post Your Answers

User Email:

User Name:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher