简体   繁体   English

奇怪的Java变量声明

[英]Weird Java variable declaration

I came across some code and saw a very weird way of declaring a variable 我遇到了一些代码,看到了一种声明变量的非常奇怪的方法

double var1 ();

Has anyone come across java code written in this manner? 有没有人遇到以这种方式编写的Java代码? I am not sure what this actually does, the thing that confuses me and is new to me is the space between the variable name and "()". 我不确定这实际上是做什么的,令我感到困惑和陌生的是变量名和“()”之间的空格。

Surrounding Code: 周围的代码:

void setVar1 (double Var1);
double var1 ();

I actually think this maybe a method declaration actually instead of a variable declaration. 我实际上认为这实际上是方法声明而不是变量声明。 Opinion? 意见?

The only way that would be valid is if it was a method declaration inside an interface. 唯一有效的方法是,如果它是接口内的方法声明。 For example 例如

interface What {
    double var1();
}

It's not a field. 这不是一个领域。

Here is the official Java tutorial on interfaces. 这是有关接口的官方Java教程。

the thing that confuses me and is new to me is the space between the variable name and "()". 令我感到困惑和陌生的是变量名和“()”之间的空格。

Whitespace characters are delimiters between Java syntax elements. 空格字符是Java语法元素之间的分隔符。 You can put as many such spaces as you want 您可以根据需要放置任意数量的空间

interface What {
    double var1                                                 ();
}

or even 甚至

interface What {
    double var1  




                                  ();
}

当您在接口中进行方法声明时,可能会发生这种情况

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

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