Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2011, 07:32 PM   PM User | #1
Phoncible
New Coder

 
Join Date: Aug 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Phoncible is an unknown quantity at this point
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).
Phoncible is offline   Reply With Quote
Old 08-08-2011, 09:19 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,661
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
calling, function, method, prefix

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:10 PM.


Advertisement
Log in to turn off these ads.