redeclare static variable in the inherited class

redeclare static variable in the inherited class

can we redeclare static variable in the inherited class like this:

public class StaticClass1 {

 class StaticClass1{

    static int noOfInstances;

    StaticClass1(){

        noOfInstances++;

    }

}

 class StaticClass extends StaticClass1

{   public static void main(String[] args){

        StaticClass1 sv1 = new StaticClass1();

        System.out.println("No. of instances for sv1 : " + sv1.noOfInstances);

        StaticClass1 sv2 = new StaticClass1();

        System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);

        System.out.println("No. of instances for st2 : "  + sv2.noOfInstances);

        StaticClass1 sv3 = new StaticClass1();

        System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);

        System.out.println("No. of instances for sv2 : "  + sv2.noOfInstances);

        System.out.println("No. of instances for sv3 : "  + sv3.noOfInstances);

                **//here i have redeclared the static variable as "25" is it possible,then what is static variable**

                noOfInstances=25;


        StaticClass1 sv4 = new StaticClass1();

        System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);

        System.out.println("No. of instances for sv2 : "  + sv2.noOfInstances);

        System.out.println("No. of instances for sv3 : "  + sv3.noOfInstances);

        System.out.println("No. of instances for sv4 : "  + sv4.noOfInstances);

        StaticClass1 sv5 = new StaticClass1();

        System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);
        System.out.println("No. of instances for sv2 : "  + sv2.noOfInstances);
        System.out.println("No. of instances for sv3 : "  + sv3.noOfInstances);
        System.out.println("No. of instances for sv3 : "  + sv4.noOfInstances);
        System.out.println("No. of instances for sv3 : "  + sv5.noOfInstances);
    }

}


//}
View Answers

Ads









Related Tutorials/Questions & Answers:
Example of a class variable (static variable)
Example of a class variable (static variable)   ... you can define the static class variable in a class. When a number of objects... of a static variable first of all create a class StaticVariable. Define one static
Static variable in java
methods, variable inside a class and static class. Static variable is also called class variable which belongs to class not to object. Static.... Static variable is accessed without creating object of class using class name
Advertisements
Static Variable
of a class, an object, is created. When static is applied to member variables, it means that the static variable is shared among all instances of the class...Static Variable  What is the basic use of introducing static variable
static
redeclare static variable in the inherited class  can we redeclare static variable in the inherited class like this: public class StaticClass1 { class StaticClass1{ static int noOfInstances; StaticClass1
STATIC VARIABLE DECLARATION
STATIC VARIABLE DECLARATION  why cannot the static variable declare inside the method
What is the importance of static variable?
What is the importance of static variable?  hi, What is the importance of static variable? thanks
how to inherit static variable
how to inherit static variable   how to inherit static variable or methods in Java?? Please explain the entire process of inheriting the variable. Thanks in advance
What is a local, member and a class variable?
What is a local, member and a class variable?   Hi, What is a local, member and a class variable? thanks
About Static Class
About Static Class  is it neccessary to create object for static class
Can a class be declared as static?
Can a class be declared as static?  Hi, Can a class be declared as static? thanks
non static variable cannot be referenced from static context
non static variable cannot be referenced from static context  public class Add{ int foo=7; int bar=8; public int add(int x,int y... "non static variable cannot be referenced from static context". How to solve
non static variable cannot be referenced from static context
non static variable cannot be referenced from static context  public class Add{ int foo=7; int bar=8; public int add(int x,int y... public static void main(String []s){ Add a1=new Add(); a1.add(7,8
Can we declare a static variable inside a method?
Can we declare a static variable inside a method?   Hi, Can we declare a static variable inside a method? thanks
Java inner class and static nested class
Java inner class and static nested class  Java inner class and static nested class
static keyword in java
of static method:- <class-name>.<method-name> static variable... single copy of all instance of a class. In a static variable we can directly... varName = value; Example of static method and static variable class StaticTest
What is the difference between a static and a non-static inner class?
What is the difference between a static and a non-static inner class?   Hi, What is the difference between a static and a non-static inner class? Thanks
How to pass variable from one class to another in java
How to pass variable from one class to another in java  How to pass variable from one class to another in java?   Example: public class...; } } //Another class Class2. public class Class2 { public static void main(String
how to set the class path variable in tomcat ?
how to set the class path variable in tomcat ?  description... for the useBean class attribute action.TestBean is invalid...;jsp:useBean id="test" class="action.TestBean" /> <jsp:setProperty name
ModuleNotFoundError: No module named 'django-static-class-minifier'
ModuleNotFoundError: No module named 'django-static-class-minifier'  ...: No module named 'django-static-class-minifier' How to remove the ModuleNotFoundError: No module named 'django-static-class-minifier' error
Instance variable in java
in java, they are as follows : Local variable Instance Variable class/static variable Instance variable : Instance variable are declared in a class but outside method or constructor. Instance variable are created when
Access Static Member Of The Class Through Object
Access Static Member Of The Class Through Object  ... that work without any object of the class. Static methods are limited to calling other static methods in the class and to using only static variables
static
, The Static means that a certain object/variable is resident in memory and accessed each time an instance of a class, an object, is created. When static is applied to member variables, it means that the static variable is shared among all
Squaring of two number in Static class
Squaring of two number in Static class  ...; . To square a static method  are define main class and passing the two... static method and how can display multiplication of two any number. The static
Using Abstract Class
class. This class must be inherited. Unlike interface the abstract class may... but we can call its static method by its class name. The code...Using Abstract Class      
Example of a instance variable
class,  the same copy of  instance variable is provided to all... we have used in the constructor of a class. We can't call the non-static variable in a main method. If we try to call the non-static method in the main
how to pass variable from simple java main class(not servlet) to the jsp page?
how to pass variable from simple java main class(not servlet) to the jsp page?  I have a simple Java class in which I invoke a call to a JSP page... from the simple Java class to the JSP page. How can I do that? Here is my
static
static  What is the exact need of declaring a variable as static?   A static variable is a variable who's single copy in memory is shared by all objects,so any modifications to the static variable will modify it's value
Static Nested Classes
; A nested class that is declared static is called a static nested class... independently of any particular outer class object. A static nested class use... an object reference. A static nested class interacts with the instance
STATIC
is an example: public class StaticTest { static{ System.out.println("Hello from...STATIC  WHAT IS STATIC BLOCK?EXPLAIN , AND WHAT IS THE DIFFERENCE BETWEEN STATIC BLOCK AND STATIC METHOD?   A STATIC block is automatically
Declare Reference variable
in memory when object is created. Static variable is class variable which... variable static int b; // static variable A ref; static... of object creation, but here i am declaring reference variable in class
static
instance of that object (the object that is declared static) in the class... of the class that is initialized. Another thing about static objects is that you...Static Concept  in what are all the situation we may use static

Ads