[英]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();
}
}
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.methodName ,
Temp.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.