Results 1 to 2 of 2
  1. #1
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Question static method sparks error on overriding non-static method

    I have some question concerning static overriding non-static methods. I have read up about "static" and have got to understand it as something that is attached to the class and all objects of the class can only refer and if not final, change the value, which affects the value of all the other objects.

    The question I have is that if there is a scenario like the one below:

    Java Code:
    1: class Test
    2: {
    3: void show()
    4: {
    5: System.out.println("non-static method in Test");
    6: }
    7: }
    8: public class Q3 extends Test
    9: {
    10: static void show()
    11: {
    12: System.out.println("Overridden non-static method in Q3");
    13: }
    14:
    15: public static void main(String[] args)
    16: {
    17: Q3 a = new Q3();
    18: }
    19: }
    Why do we get a compilation Err at line 10? Why cant static methods override the other method.

  2. #2
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Thumbs up static - non static override

    On this page tou can see the possible overrides in Java: java.sun.com/docs/books/tutorial/java/IandI/override.html

    As you can see it isn't possible to override static with non static, or override non static with static because one refers to the object and the other to the class.

    the only solution is to make both method static or both non static

Similar Threads

  1. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 AM
  2. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 09:51 AM
  3. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 01:38 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •