• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in static.

 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i have studied that a static method or variable is always associated with the class and can not be accessed with the object reference. i was trying this in a program, but the object of the class in which static is defined is able to acces the method. how is this possible. pl. clearify my problem and about static method and static class. the code is as follows -

<CODE>

class sachin
{

static void h() //static method
{
System.out.println("in static sachin");

}
};

public class test extends sachin //extend class
{
public static void main(String[] args)
{
test t=new test();
sachin s= new sachin();
s.h();
t.h();
}
}


</code>
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Accessing static members in a static way (using the clas name) is the preferred way to do it. Why? It's mainly a matter of clarity -- accessing the static members the other way is misleading because it looks like you are acting on the given object.
 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but bro my question is that can we access a static method using the instatnce of class in which the static method is defined???
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It can be accessed. IDEs like Eclipse will give a warning but nothing stops you from compiling and running the code.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can access a static method or member variable through an instance.

Does anybody know why this is allowed in Java? I can't think of any example that demonstrates why it would ever be necessary to do this. In my opinion, this should have been made illegal in Java.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper de Jong:
Yes, you can access a static method or member variable through an instance.

Does anybody know why this is allowed in Java? I can't think of any example that demonstrates why it would ever be necessary to do this. In my opinion, this should have been made illegal in Java.


I agree that it should have been illegal. The problem is that the method that gets invoked is the one in the compile-time type of the instance, whereas a casual reader of the code (not realising that the method is static) would think that the run-time type is used.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
I agree that it should have been illegal. The problem is that the method that gets invoked is the one in the compile-time type of the instance, whereas a casual reader of the code (not realising that the method is static) would think that the run-time type is used.


As an example of what's misleading about this syntax, consider the following. Will running Test's main load class B into the JVM? Why or why not?
 
mert ´┐Żzkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I found some information from someone.
Maybe , this can be beneficial for you.
Perhaps you're thinking of "non-static whatever cannot be referenced from a static context"? That's the opposite of what you have here. When you're in static context (e.g., inside the body of a static method), you can't refer to any non-static methods or member variables because they don't exist--they require an instance, a this, and there is no this in a static context.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic