[英]is it possible to call a function without it's name?
I tried calling a function in my code, but i get a blank screen only. 我尝试在代码中调用一个函数,但仅出现空白屏幕。
class Addition
{
public void add(){
int a=20;
int b=30;
int c=a+b;
System.out.println("Addition of a and b is: " +c);
}
}
public class Methodcall
{
public static void main(String args[])
{
Addition call=new Addition();
}
}
I expect the addition of a & b But all i get is just a blank screen only 我希望添加a&b但我得到的只是一个空白屏幕而已
The code wouldn't do what you're expecting it to do: 该代码不会执行您期望的操作:
In your main
function, you're only instantiating the Addition
object. 在您的main
功能中,您仅实例化Addition
对象。 Think of it as creating the object: 将其视为创建对象:
Addition call = new Addition();
If you want it to do something , in your case to add a
and b
and print the result, you will need to call the method: 如果您希望它做某事 (在您的情况下)添加a
和b
并打印结果,则需要调用方法:
call.add();
As others have mentioned, you could put it in the constructor, but that's not what the constructor is meant for. 正如其他人提到的,您可以将其放在构造函数中,但这不是构造函数的目的。
Addition call=new Addition();
call.add();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.