Results 1 to 8 of 8
  1. #1
    blug is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default Referring to a super class

    Hey, I was just asked a question out of a text book I am reading and I cannot remember for the life of me how to refer to a super class when you are writing within a static method.

    I know the first thought is super.methodName(), but that doesn't seem to work from within a static method.

    Essentially, I am writing a class called "ArrayListAddition" which inherits everything from "ArrayList". Essentially ArrayList is a class which creates an array which you have additional methods to do things which you can't do with a normal array.

    \I was thinking of writing something like this as the static method within the derived class.

    PHP Code:
    //Method which removes the first element of the list
    	public static void delete(){
    		super.remove(0);
    	}
    It's not allowing me to do it for some reason though.. Saying I can't refer to a super class within a static method.

    This is really bugging me, I have an assignment coming up and I get the feeling this is going to be a big part of it.

    Thanks!
    Last edited by blug; 03-20-2011 at 05:07 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why dies the method have to be static? are you calling this method to remove the first item from a static arraylist? If so you can simply do staticArrayList.remove(0) in the method call.

  3. #3
    blug is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Well, I'm not exactly trying to put a practical side to this method. You could simply just call the remove(0) outside the method, but meh, still want to know. The question asks for it to be a static method as well.

    Is there anyway you can do it? Or am I looking at it wrong..

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't believe there is way to refer to a super class from a non static variable. Is the method supposed to take any arguments? You can have it take an array list as an argument and then call remove(0) on that argument. Others may have an answer, but to the best of my knowledge you can't refer to a super class from a static method.


    What is the exact question btw?

  5. #5
    blug is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Wait I think I'm starting to remember now, maybe this might be a bit of a lesson for you to.

    You can't refer to a super class from a static method because a static method refers to the class and not an object. It would be pointless to refer to a class when the methods can't even retrieve instance variables or anything like that.

    When reading the question I'm starting to think you are correct. I'm thinking you have to list a arrayList type as an argument, I think I misread it a bit.

    Thanks for the help, I didn't actually consider that perhaps you had to use an argument.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Haha, my problem tends to be that I have a hard time explaining the details of how static works even though I do understand how they work. Static methods tend to be like helper methods, think square root, ceiling, floor, round.

    Glad I helped you figure out what you wanted.

    When thinking of your question I thought that you wanted a helper method for removing the first element in an arraylist, instead of typing arraylist.remove(0) you can just type delete(arraylist).

  7. #7
    blug is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Heres the answer out of interest..

    PHP Code:
    //Method which removes first occurence of object from list
    	public static void delete(ArrayListAddition array, Object object){
    		for (int i = 0; i < array.size(); i++){
    			if(object.equals(array.get(i))){
    				array.remove(i);
    				i = array.size();
    			}
    		}
    	}

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad I was helpful, please mark your thread solved with the thread tools at the top if you are finished.

Similar Threads

  1. Replies: 5
    Last Post: 01-26-2011, 07:38 PM
  2. Super class refering to subclass
    By sujith123 in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 10:00 AM
  3. super instanceof Class?
    By mikeiz404 in forum New To Java
    Replies: 11
    Last Post: 01-23-2009, 08:23 PM
  4. Super class and Subclass in same source file
    By makbar24 in forum New To Java
    Replies: 17
    Last Post: 09-10-2008, 02:24 PM
  5. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 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
  •