aspose file tools*
The moose likes Beginning Java and the fly likes problem in static. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in static." Watch "problem in static." New topic
Author

problem in static.

sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
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

Joined: Sep 16, 2005
Posts: 1780

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.


There is no emoticon for what I am feeling!
sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
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

Joined: Sep 06, 2003
Posts: 259
Yes. It can be accessed. IDEs like Eclipse will give a warning but nothing stops you from compiling and running the code.


http://www.skillassert.com


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jan 26, 2006
Posts: 33
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in static.
 
Similar Threads
Wildcard Insantiation warped????
Generic methods doubt # 2
javabeat generics question doubt
ArrayList clone question
Generics error, can not understand