-
Junior Member
Core java
Hi My question is What is static method in java?
-
Expert Member
Re: Core java
Static methods use no instance variables of any object of the class they are defined in. If you define a method to be static, you will be given a rude message by the compiler if you try to access any instance variables. You can access static variables, but except for constants, this is unusual. Static methods typically take all they data from parameters and compute something from those parameters, with no reference to variables. This is typical of methods which do some kind of generic calculation. A good example of this are the many utility methods in the predefined Math class
-
Junior Member
Re: Core java
Static methods are executed before creating of object.so,instance variables will not be available to static methods,only static variables will be available.The execution series is first Static Block ,second Static Method Then Any other methods.
We will define main method as static because with out creating object we cannot call any method and with out main method we can not create object.so we are defining main method as static.
-
Junior Member
Re: Core java
nice question.....
For every class....for it to be executed it needs some memory in the RAM.
So,as soon as JVM encounters a new class name..it needs the information of the class before an object for the class is created.
So, as soon as it encounters a new class name...the JVM creates the context(some memory for the class known as "context") of the class.
in this context of the class,
the JVM loads all the static information that is present in the class.. i.e., static variables,static methods and static blocks are placed into this context.
bcoz of this special memory of the class...as already all the static methods,fields and blocks are available.. JVM don't need any object to call this methods or fileds.
hence we can also refer static methods,fields using the class name.
and the answer to the question is that the static methods need not to be called using the object.so before creating the object also we can access static methods.
The requirement to go for the static methods is the same.
-
Junior Member
-
Junior Member
Re: Core java
Hello Geeks users ,i am a new user
please let me join with u people
-
Junior Member
Re: Core java
hello can u explain what is thread?
-
Junior Member
Re: Core java
static method is a method for which we need not create an object . we can invoke this by using class reference .of course you can call it by using object.
static methods are initialized when the source code is loaded in jvm i.e. before creating an object.
code:
class Name{
static void call(){
S.o.p("----");
}
public static void main(String args[]){
Name n;
n.call();//here we have called it before creating an object
}
}
-
Junior Member
Re: Core java
Thread is a dispatchable unit of work.
or u can say execution of a block of statements in a program.
ex:
void display(){
-----------//statement1
-----------//statement2
-----------//statement3
-----------//statement4
}
we can say the above 4 statements executes under a thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules