• Post Reply Bookmark Topic Watch Topic
  • New Topic

lil explanation requied ..  RSS feed

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys ,
i have prb understanding these ..can anybody pls explain this to me .

"note that a static method in a class can always use a reference of the class's type to access its members, regardless of whether these members are static or not."




"Only an instance method can be declared abstract. Since static methods cannot be overridden, declaring an abstract static method would make no sense.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Nayak

<b>"note that a static method in a class can always use a reference of the class's type to access its members, regardless of whether these members are static or not."</b>

This statement is just Strongly worded. The gist of it is that you CANNOT access non-static members of a class from a static method.

The only way out though is through an instance. Think about it. Can you access a class's non-static variable or a method witout first instatntiting the class from your main method, which by the way is static.

consider this simple class

<CODE>
public class A {

public static void staticMethod() {
System.out.println("Inside Static Method");
}

public void instanceMethod() {
System.out.println("Inside Instance Method");
}

public static void main(String [] args) {
A.staticMethod(); // can access it because it is a static method

staticMethod(); // can still access it, because static methods can
// access other static members of the same class
// without qualifying them with the class name

instanceMethod(); // CANNOT! Because static methods cannot invoke or
// other non-static members

A a; // COMPILES

a.instanceMethod(); // Compile time error. a might not have been
// initialized

A.instanceMethod(); // CANNOT! Because it is not a class method
// Compile time Error
// non-static method cannot be referenced from
// a static context

A x = new A(); // Create a new instance of the class

x.instanceMethod(); // WE CAN! Through An Instance

} // END OF MAIN
} // END OF CLASS
</CODE>

Now for the second statement

<b>Static methods cannot be overridden. They can only be hidden.</b>

static methods are class methods.They provide services on behalf of the class. You do not need an instance of the class to access a static method.
Now, if a class declares a static method as abstract which does not have any implementation, then how can it provide a service on behalf of the class?

It cannot, Right?

So, there we are, you can't declare a static method as abstract.
IT IS A COMPILE TIME ERROR.
JDK 1.4 responds thus when i compile staticExample.java

<CODE>
abstract class simpleClass{
abstract static void method();
}

public class staticExample extends simpleClass{
static void method(){
System.out.println("Method in base class");
}

public static void main(String [] args){
staticExample s = new staticExample();
s.method();
}
}
</CODE>

Compiler Response

C:\JavaPrograms\staticExample.java:2: illegal combination of modifiers: abstract and static
abstract static void method();
^
1 error

Hope this helps


am i right?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch "chowdary T A".

Would you please change your displayed name to one of the following formats:

{first name} {family name}
{initial(s)} {family name}
{first name} {initial(s)} {family name}

To learn more about JavaRanch's Naming Policy you can click here

Thanks
-Barry (moderator/bartender)
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx alot chowdary... this helped me alot .
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's helpful, thanks!
 
expectation is the root of all heartache - shakespeare. tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!