What is static variable and static method?

Answers were Sorted based on User's Feedback



What is static variable and static method?..

Answer / rajesham

static variables are global variables for which memory is
allocated on loading of class.
for this variable memory allocated only once,but use more
than once.
static method is a class method.
static method performs class operation.
static method can not operate instance data directly.
static method is use without "this" reference.
static method is bind with class name or object name

Is This Answer Correct ?    2 Yes 0 No

What is static variable and static method?..

Answer / abanish kumar rajoot

Static Varaibles & methods both are, class level
declaration means of it ,static varaibles are those which
are common for all the objects and the advantage of static
varaible over non-static is that they allocate memory space
at complitaion time means when a class gets load in memory.
and they remains in memory till class is loaded there.
Static methods are declared with static keyword and they
can be accessed by class name and only accessed static
variables.

Is This Answer Correct ?    2 Yes 0 No

What is static variable and static method?..

Answer / masterty

Please guys, answer in right way! Do not write the bull of
sh**t! Answer to the question - What is static variable and
static method? That's it! Do not answer to the half of question!

Is This Answer Correct ?    7 Yes 6 No

What is static variable and static method?..

Answer / pankaj

just im telling about
-> what is static method in java
Ans ->static method represent class behaviour and instance variable represent object behaviour ..but instance of a class is not require for the invokation of static varibale.
-> static data member of the class can be referd in a static block or methd.
-> static block and static method can directly invoked the static method ,nonstatic method can not directly by static method or static block.
->this and supper keyword can not be use in static block and method

Is This Answer Correct ?    1 Yes 0 No

What is static variable and static method?..

Answer / monika watts

STATIC VARIABLE: static variable is that variable that is
shared by all the objects of a class.means allocates memory
once even if we create any no of objects and even if we
create no object.
STATIC METHOD: first thing about static method is that we
can access static variables only with in static method.
and static methods can be called without the object of that
class.
let
class a
{
static void hello()
{
System.out.println("hello friend");
}
}
now we can access this method as hello() or a.hello();
without any object or with class name.

Is This Answer Correct ?    2 Yes 1 No

What is static variable and static method?..

Answer / jai

One Copy of Static variable will be shared by multiple
instances.Static method can be called by the Class itself
without creating any instance where as the non static
methods cannot be called without the help of instances.

Is This Answer Correct ?    1 Yes 0 No

What is static variable and static method?..

Answer / sivaramakrishna bandaru

static method or a variable is not attached to a particular
object, but rather to the class as a whole. They are
allocated when the class is loaded. Remember, each time you
call the instance the new value of the variable is provided
to you.

Is This Answer Correct ?    1 Yes 0 No

What is static variable and static method?..

Answer / nagina

static variable:
It is a variable which belongs to the class and not to
object(instance).Static variables are initialized only
once , at the start of the execution . These variables will
be initialized first, before the initialization of any
instance variables. A single copy to be shared by all
instances of the class. A static variable can be accessed
directly by the class name and doesn’t need any object
Syntax : <class-name>.<variable-name>
static method:
It is a method which belongs to the class and not to
the object(instance). A static method can access only
static data. It can not access non-static data (instance
variables). A static method can call only other static
methods and can not call a non-static method from it.
A static method can be accessed directly by the class name
and doesn’t need any object
Syntax : <class-name>.<method-name>
A static method cannot refer to "this" or "super" keywords
in anyway

Is This Answer Correct ?    1 Yes 0 No

What is static variable and static method?..

Answer / kishore rajkumar

When static is used the VARIABLE or PROPERTY in the class is initialised only once. However, that does'nt mean that the value of the VARIABLE will remain unchanged. What i mean to say is, no matter how many instances of the class you create, your VARIABLE will not be re-initialized to its initial value for each new instance as opposed to a non-static variable.

Let us see an example;


public class StaticTest{

int a; // a non-static variable
static int b; // variable declared as static

StaticTest(){
a++;
b++;
}


public void display(){

System.out.println("a="+a);
System.out.println("b="+b)

}


}

class MainTest{

public static void main(String[] args){

StaticTest o1=new StaticTest();
StaticTest o2=new StaticTest();
StaticTest o3=new StaticTest();

o1.display();
}

}

output:
-----------------------------
a=1;
b=3;

EXPLAINATION:
===============

Look in the class " StaticClass ". Here, we have declared two variables 'a' & 'b'. Variable 'a' is a non-static variable whereas 'b' is declared as static.
Also, each time instance of this class(StaticClass) is created 'a' & 'b' are incremented inside the constructor.
Now, in the ' MainTest ' class we have created 3 seperate instances of the class ' StaticClass '.
Here, for each new instance the variable 'a' is re-initialized to its initial value and then incremented by 1.
As a result, its value is always 1 no matter how many instances we create.

But, in case of 'b' the value in not re-initialized everytime a new instance is created. Rather, it is initialized to its initial value only for the first instance and then its value is obtained by all the subsequent instances.
Hence, its value keeps on increasing as more and more instance are created.

Is This Answer Correct ?    1 Yes 0 No

What is static variable and static method?..

Answer / neha siddiqui

Static:-
The main is always declare as s Static, since
interpret uses this method before any object are created....

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

What type of language is java?

0 Answers  


What is the buffer limit?

0 Answers  


What is the final keyword in java?

0 Answers  


What is the purpose of methodology?

0 Answers  


What checkbox method allows you to tell if a checkbox is checked?

0 Answers  






What is the r character?

0 Answers  


What environment variables do I need to set on my machine in order to be able to run java programs?

0 Answers  


Can we call the run() method instead of start()?

0 Answers  


What is the dot operator?

0 Answers  


In a my eclipse editor if i want to switch from jdk 1.4 to jdk 1.6 how to do that???? Initially i have jdk1.4

1 Answers   Tech Mahindra,


Why wait and notify methods are declared in object class?

0 Answers  


Why is core java important?

0 Answers  


Categories