简体   繁体   English

JAVA方法调用查询

[英]JAVA Method Calling Query

class Temp
{
    private Temp(int data)
    {
        System.out.printf(" Constructor called ");
    }
    protected static Temp create(int data)
    {Temp obj = new Temp(data);
        return obj;
    }
    public void myMethod()
    {
        System.out.printf(" Method called ");
    }
}

public class Test
{
    public static void main(String[] args)
    {
        Temp obj = Temp.create(20); //How this is a method call?
        obj.myMethod();
    }
}
  1. In the above program, the commented line is not understandable for me? 在上面的程序中,注释行对我来说不容易理解吗? Can any one have good explaination about how static method is called using object creation 任何人都可以很好地说明如何使用对象创建来调用静态方法

A static method means that it can be called without creating an Object of that class, in your example Temp item. static方法意味着在您的示例Temp项中,无需创建该类的Object即可调用它。

 static Temp create(int data)

The static word in your method is what is allowing you to do that. 您方法中的static字词是允许您执行此操作的要素。 The method is then callable in a static way, which means using className.methodName , in your example, Temp.create() 然后可以以静态方式调用该方法,这意味着在您的示例中使用className.methodNameTemp.create()

And since your method is returning a Temp object, you are putting that into a Temp object 而且由于您的方法返回的是Temp对象,因此您将其放入Temp对象

Temp obj = Temp.create(20);

In the Temp obj you are putting the result of the object created in your method, in the line Temp obj中,将在方法中创建的对象的结果放在一行中

{Temp obj = new Temp(data);
    return obj; //this is your object
}
class Temp
{
    private Temp(int data)
    {
        System.out.printf(" Constructor called ");
    }
    protected static Temp create(int data)
    {Temp obj = new Temp(data);
        return obj;
    }
    public void myMethod()
    {
        System.out.printf(" Method called ");
    }
}

public class Test
{
    public static void main(String[] args)
    {
        Temp obj = Temp.create(20); //How this is a method call?
        obj.myMethod();
    }
}

A constructor of a class always follows a same signature: [access modifier -> protected, public, private, default] [name of the class] (parameters) 类的构造函数始终遵循相同的签名:[访问修饰符->受保护的,公共的,私有的,默认的] [类的名称](参数)

So, in your code, 因此,在您的代码中

private Temp(int data) {
  System.out.printf(" Constructor called ");
}

is your constructor. 是您的构造函数。

In order to use your Temp class, at least the non-static members of it, you'll need an instance of the class to be able to use it, but, since your constructor is declared private, an instance can only be created within the very same class. 为了使用您的Temp类,至少是它的非静态成员,您需要一个该类的实例才能使用它,但是,由于您的构造函数被声明为私有,因此只能在内部创建一个实例同一个班级。

That is what you do here: 那就是你在这里做的:

protected static Temp create(int data)
        {Temp obj = new Temp(data);
            return obj;
        }

This method is a static method, meaning it 'exists' as soon as the class is loaded in the memory, even without the class being instantiated. 该方法是静态方法,意味着即使没有实例化该类,只要将类加载到内存中,它就会“存在”。 Since it is declared protected, not private, it can be used by subclasses of Temp , and by classes that are in the same package as Temp , like your Test class. 因为它被声明的保护,而不是私人的,它可以通过子类使用的Temp ,并通过了在同一个包中的类Temp ,喜欢你的Test类。

Your Test class calls this method, which calls the constructor, and returns the created instance to the Test class. 您的Test类调用此方法,该方法调用构造函数,并将创建的实例返回给Test类。 This way of working is used in certain scenario's, for instance if you want to limit the number of instances created per VM, like with the Singleton pattern. 例如,如果您想限制每个VM创建的实例数量(例如使用Singleton模式),则可以在某些情况下使用这种工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM