Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Method call without class or object name prefix

    I've seen it done in practice where a method is called just by it's method name, without the classname. or objectname. prefix.
    I.e. a method used like: someMethod();
    not
    class.someMethod(); or object.someMethod();
    Also, the method I saw used in this manner was not declared static. It was also in an imported package, not from the class it was being called in. It looked something like:
    public boolean method(){...}

    When I tried to reproduce the effect I couldn't get it to work, at all. If method is non-static i get the "blah blah blah static context non-static call" errors. If method is static is says i need the class name prefix to call properly. What's going on that just using method name is allowed and how can I make it do that (even if it's unnecessary I just want to know the how's and why's).

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,027
    Thanks
    4
    Thanked 2,668 Times in 2,637 Posts
    The error you have there is that you are trying to use a static method from a dynamic context. This cannot be done since it doesn't know what you are trying to do - non-static methods cannot be called statically as they require a context of an object in which to work.
    See this example:
    PHP Code:

    public class StaticTest
    {
        public static 
    void testStatic()
        {
            
    staticCall("This is a static call"); // Chain from a static to a static
        
    }
        
        public 
    void testDynamic()
        {
            
    dynamicCall("This is a dynamic call"); // Chain from a dynamic to a dynamic
        
    }
        
        public static 
    void staticCall(String sStatic)
        {
            
    System.out.println(sStatic);
        }
        
        public 
    void dynamicCall(String sDynamic)
        {
            
    System.out.println(sDynamic);
        }
        
        public static 
    void main(String... argv)
        {
            
    StaticTest s = new StaticTest();
            
            
    s.testDynamic(); // requires a object context since this is static method
            
    testStatic(); // This is a static method so no explicit class could be chosen
        
    }

    Personally, I resolve all methods and members to either the class name or this. I do this simply because languages like PHP to not accept variable masking in OOP, so they are required to resolve the method to the class or context.


 

Tags for this Thread

Posting Permissions

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