Results 1 to 3 of 3
  1. #1
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default 'Class' Object and calling Static Methods?

    Is there a way to call static methods of a class from the Class Object (without creating a new instance of the class)?

  2. #2
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    The question is very vague. I'm guessing you want to know what the class Object is all about and how to call static methods?

    In Java, Object is the superclass that all other classes subclass. This happens behind the scenes automatically and can not be bypassed (unless you write your own java compiler and JVM I suppose). As a result every class has some basic functionality even if you don't define any, such as the toString method.

    To call a static method, the syntax is {class}.{method}.

    For example, Runtime.getRuntime() returns the default Runtime object.
    moved conversation from other forum
    Last edited by mikeiz404; 01-24-2009 at 05:11 AM. Reason: moved conversation from other forum

  3. #3
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    Actually, I'm referring to the 'Class' Class. I wasn't too clear and the very name lends itself to confusion.
    So...
    Java Code:
    this.getClass()
    Here is the code I have done so far:
    Java Code:
    	public LinkedList<Class> getFilterLevels(){
    		Class parent = this.getClass();
    		LinkedList<Class> l = new LinkedList<Class>();
    		while(!parent.equals(Filter.class)){ //Filter is the 'root' class
    			l.add(parent);
    			parent = parent.getSuperclass();
    		}
    		return l;
    	}
    
    	public static boolean evaluate(Packet p){
    		return true;
    	}
    
    	public boolean isTrue(Packet p){
    		//code to implement: calls evaluate on all parent classes
    	}
    I am trying to create a Filter class which can be extended. The idea is to extend upon existing filters and then provide unique filtering code which filters in a more specific way.
    I am trying to keep the specific filter code for each filter in separate methods so that when many version of the Filter class are used, if one filter happens to be a parent of another then that same filtering code doesn't have to be needlessly reprocessed. Does this make sense?

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 12:58 PM
  3. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 AM
  4. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 12:24 AM

Posting Permissions

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