惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
J
Java Code Geeks
Vercel News
Vercel News
A
About on SuperTechFans
G
Google Developers Blog
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
博客园 - 司徒正美
S
SegmentFault 最新的问题
D
DataBreaches.Net
博客园_首页
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
量子位
雷峰网
雷峰网
IT之家
IT之家
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
博客园 - 三生石上(FineUI控件)
H
Help Net Security
宝玉的分享
宝玉的分享
博客园 - 叶小钗

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
Java Musings - referencing an uninitialized final variable
Ashish Bhatia · 2017-10-02 · via ashishb.net

Java has fewer quirks compared to C++, but sometimes I do come across surprises. A code like following will fail to compile since you are trying to initialize a variable with an uninitialized variable.

1
2
3
4
5
6
7
8
9
public class Sample {

  private final String mField1;
  private final String mField2 = mField1 + " two";

  private Sample(String field1) {
    mField1 = field1;
  }
}

But if instead of directly referencing mField1, you reference indirectly via a getter method code will compile, and mField2 will get null value for mField1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Sample {

  private final String mField1;
  private final String mField2 = getField1() + " two";

  private Sample(String field1) {
    mField1 = field1;
  }

  private String getField1() {
    return mField1;
  }

  private String getField2() {
    return mField2;
  }

  public static void main (String[] args) {
    Sample sample = new Sample("one");
    // prints "Field 1 is one"
    System.out.println("Field 1 is " + sample.getField1());
    // prints "Field 2 is null two"!!!
    System.out.println("Field 2 is " + sample.getField2());
  }
}