我下面有两节课。两者都有一个带有 getter 的变量“回复”。此变量没有设置方法。唯一的区别是在 ClassOne 中,变量是 static final 的。
那么有什么区别,哪个更受欢迎?
public class ClassOne {
    private static final String reply = "Success";
    ..
    public String getReply() {
        return reply;
    }
    // no setter
}
Run Code Online (Sandbox Code Playgroud)
和 2 类
public class ClassTwo {
    private String reply = "Success";
    ..
    public String getReply() {
        return reply;
    }
    // no setter
}
Run Code Online (Sandbox Code Playgroud)
更新1:
我想知道的是,当变量没有setter时,是否应该将变量声明为static final以进行优化?还是没关系?
请看下面的代码
private void displayInitialRevenue_Method() {
    //Get the dates from the combo
    String selectedCouple = revenueYearCombo.getSelectedItem().toString();
    if (selectedCouple.equals("Select Year")) {
        return;
    }
    String[] split = selectedCouple.split("/");
    //Related to DB
    double totalAmount = 0.0;
    //Get data from the database
    dbConnector = new DBHandler();
    dbConnector.makeConnection();
    DefaultTableModel model = (DefaultTableModel) initialRevenueTable.getModel();
    model.setRowCount(0);
    ResultSet selectAllDetails = dbConnector.selectAllDetails("SQL CODE ");
    try {
        if (selectAllDetails.isBeforeFirst() == false) {
            JOptionPane.showMessageDialog(null, "This table is empty");
        } else {
            while (selectAllDetails.next()) {
                String clientName = selectAllDetails.getString("Client Name");
                String providerName = …Run Code Online (Sandbox Code Playgroud) 我的问题与这个基本相同,但这也适用于static函数吗?
我想了解:
static中的所有函数都视为?finalfinalfinal关键字有什么作用吗?staticfinal为什么 Java 允许这样做,
class Test {
    boolean a;
    public void test() {
        ...
        object.method(e -> a = true);
    }
}
Run Code Online (Sandbox Code Playgroud)
但不是这个,
class Test {
    public void test() {
        boolean a;
        ...
        object.method(e -> a = true);
    }
}
Run Code Online (Sandbox Code Playgroud)
对于第二个示例,它抛出:
local variables referenced from a lambda expression must be final or effectively final
第二个例子的唯一区别是变量是在方法内部而不是类本身声明的。我是 Java 编程的初学者,我是否遗漏了一些明显的东西?
我计划编写一个类,extends java.time.YearMonth目的是YearMonth使用一种方法来扩展LocalDates ,该方法使我能够流式传输其中的s YearMonth:
public class ExtendedYearMonth extends YearMonth {
    public Stream<LocalDate> days() {
        LocalDate firstOfMonth = this.atDay(1);
        LocalDate lastOfMonth = firstOfMonth.with(TemporalAdjusters.lastDayOfMonth());
        return firstOfMonth.datesUntil(lastOfMonth);
    }
}
Run Code Online (Sandbox Code Playgroud)
好吧,当我发现YearMonth是一个final class? final classes 不可扩展。
我当然可以写一个这样的类
public class ExtendedYearMonth {
    private YearMonth yearMonth;
    // parameterized constructor, getters and setters omitted for brevity
    public Stream<LocalDate> days() {
        LocalDate firstOfMonth = this.yearMonth.atDay(1);
        LocalDate lastOfMonth = firstOfMonth.with(TemporalAdjusters.lastDayOfMonth());
        return firstOfMonth.datesUntil(lastOfMonth);
    }
}
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的,因为它需要我将 aYearMonth …
在C++中,据我所知,将const参数添加到这些不会改变的变量和这些返回值的方法是一个很好的做法,例如:
bool ExpenseManager::checkCategory(const string &userName, const string &categName) const{}
Run Code Online (Sandbox Code Playgroud)
我的问题:final像const在c ++中一样使用Java 是一个好习惯,并声明特定的方法和/或变量final(比如为构造函数(public Something(final String haha))传递的值?
为什么即使你已经声明了一个私有的静态最终变量,一个颜色 - 默认C,让我们说 - 然后你仍然不能this.defaultC在超级构造函数中使用(即你只能使用super(defaultC)而不是super(this.defaultC),即使它只是简单的等价defaultC.
我只是试图将Tile类扩展为Wall类,而Wall类我存储了所有墙的所有必要private static final变量(例如它们的宽度和高度,另一个int和默认颜色),而Tile类已经有了一些变量(但它们protected不是private).我不希望Field hides another field弹出警告(因为Tile.java和Wall.java对于许多变量都有相同的名称),所以我使用this.了我的私有静态最终变量,并且有很多错误.
这并不是特别令人不安(因为我只是有一些警告),但我只是想知道为什么.我猜测编译器根本不喜欢它,因为你不能this在超类型之前引用,但仍然完全相同.是否还没有添加这样的功能,忽略了这样的事情,还是有其他原因我看不到你不能使用super(this.PRIVATE_STATIC_FINAL_VARIABLE);?
通常这些关键字让我感到困惑.
谁能告诉我他们到底有什么区别?
我见过很多人打来电话:
final String foo = "foo"; 
Run Code Online (Sandbox Code Playgroud)
“最终变量”。
但是,我完全不同意。当Java中的一种数据具有final修饰符时,我立即将其称为“ Constant”。变量不能为常数(完全不合逻辑)。
我错了吗?