[{"data":1,"prerenderedAt":1414},["ShallowReactive",2],{"blog-\u002Fblog\u002Fjava-classes-objects-constructors":3},{"id":4,"title":5,"body":6,"description":1400,"difficulty":1401,"extension":1402,"framework":1403,"frameworkSlug":76,"meta":1404,"navigation":157,"order":84,"path":1405,"qaPath":1406,"seo":1407,"stem":1408,"subtopic":1409,"topic":1410,"topicSlug":1411,"updated":1412,"__hash__":1413},"blog\u002Fblog\u002Fjava-classes-objects-constructors.md","Java Classes, Objects & Constructors — A Complete Guide",{"type":7,"value":8,"toc":1387},"minimark",[9,14,36,40,71,217,236,240,269,379,408,412,443,540,556,560,578,678,693,697,719,822,838,842,876,954,971,975,998,1067,1080,1084,1116,1228,1233,1237,1251,1302,1326,1330,1383],[10,11,13],"h2",{"id":12},"classes-and-objects-in-java","Classes and objects in Java",[15,16,17,18,22,23,26,27,31,32,35],"p",{},"Every Java program is built from ",[19,20,21],"strong",{},"classes",", and almost everything you do at runtime\nhappens to ",[19,24,25],{},"objects",". Before you can reason about inheritance or polymorphism, you need a\nfirm grip on the basics: what a class actually defines, how an object comes to life with\n",[28,29,30],"code",{},"new",", and the rules that govern constructors, ",[28,33,34],{},"static"," members, and visibility. Interviews\nlean on this material because it reveals whether you understand the object model or just\nmemorized syntax. This guide walks through the mechanics and the design judgment that goes\nwith them.",[10,37,39],{"id":38},"a-class-is-a-blueprint-an-object-is-an-instance","A class is a blueprint, an object is an instance",[15,41,42,43,46,47,51,52,55,56,59,60,63,64,67,68,70],{},"A ",[19,44,45],{},"class"," is a ",[48,49,50],"em",{},"blueprint"," — it declares ",[19,53,54],{},"fields"," (state) and ",[19,57,58],{},"methods"," (behavior).\nAn ",[19,61,62],{},"object"," is a concrete ",[19,65,66],{},"instance"," of that blueprint, created with ",[28,69,30],{}," and living\non the heap with its own copy of the instance fields. One class can produce many objects,\neach with independent state.",[72,73,78],"pre",{"className":74,"code":75,"language":76,"meta":77,"style":77},"language-java shiki shiki-themes github-light github-dark","class Car {\n  String color;                \u002F\u002F field — per-object state\n  void paint(String c) {       \u002F\u002F method — behavior\n    color = c;\n  }\n}\n\nCar a = new Car();             \u002F\u002F object 1\nCar b = new Car();             \u002F\u002F object 2 — separate state\na.paint(\"red\");                \u002F\u002F doesn't touch b's color\n","java","",[28,79,80,96,106,128,140,146,152,159,178,195],{"__ignoreMap":77},[81,82,85,88,92],"span",{"class":83,"line":84},"line",1,[81,86,45],{"class":87},"szBVR",[81,89,91],{"class":90},"sScJk"," Car",[81,93,95],{"class":94},"sVt8B"," {\n",[81,97,99,102],{"class":83,"line":98},2,[81,100,101],{"class":94},"  String color;                ",[81,103,105],{"class":104},"sJ8bj","\u002F\u002F field — per-object state\n",[81,107,109,112,115,118,122,125],{"class":83,"line":108},3,[81,110,111],{"class":87},"  void",[81,113,114],{"class":90}," paint",[81,116,117],{"class":94},"(String ",[81,119,121],{"class":120},"s4XuR","c",[81,123,124],{"class":94},") {       ",[81,126,127],{"class":104},"\u002F\u002F method — behavior\n",[81,129,131,134,137],{"class":83,"line":130},4,[81,132,133],{"class":94},"    color ",[81,135,136],{"class":87},"=",[81,138,139],{"class":94}," c;\n",[81,141,143],{"class":83,"line":142},5,[81,144,145],{"class":94},"  }\n",[81,147,149],{"class":83,"line":148},6,[81,150,151],{"class":94},"}\n",[81,153,155],{"class":83,"line":154},7,[81,156,158],{"emptyLinePlaceholder":157},true,"\n",[81,160,162,165,167,170,172,175],{"class":83,"line":161},8,[81,163,164],{"class":94},"Car a ",[81,166,136],{"class":87},[81,168,169],{"class":87}," new",[81,171,91],{"class":90},[81,173,174],{"class":94},"();             ",[81,176,177],{"class":104},"\u002F\u002F object 1\n",[81,179,181,184,186,188,190,192],{"class":83,"line":180},9,[81,182,183],{"class":94},"Car b ",[81,185,136],{"class":87},[81,187,169],{"class":87},[81,189,91],{"class":90},[81,191,174],{"class":94},[81,193,194],{"class":104},"\u002F\u002F object 2 — separate state\n",[81,196,198,201,204,207,211,214],{"class":83,"line":197},10,[81,199,200],{"class":94},"a.",[81,202,203],{"class":90},"paint",[81,205,206],{"class":94},"(",[81,208,210],{"class":209},"sZZnC","\"red\"",[81,212,213],{"class":94},");                ",[81,215,216],{"class":104},"\u002F\u002F doesn't touch b's color\n",[15,218,219,220,223,224,227,228,231,232,235],{},"The class is the ",[19,221,222],{},"type","; the object is a ",[19,225,226],{},"value"," of that type sitting in memory. A\nuseful test: if you find yourself saying \"",[48,229,230],{},"the"," X\" you usually mean a class; \"",[48,233,234],{},"an"," X\" or\n\"this X\" you mean an object.",[10,237,239],{"id":238},"constructors-how-an-object-is-initialized","Constructors: how an object is initialized",[15,241,42,242,245,246,249,250,253,254,257,258,260,261,264,265,268],{},[19,243,244],{},"constructor"," initializes a new object. It has the ",[19,247,248],{},"class's name",", ",[19,251,252],{},"no return type","\n(not even ",[28,255,256],{},"void","), and runs automatically when you use ",[28,259,30],{},". If you write none, the\ncompiler hands you a ",[19,262,263],{},"default no-arg"," constructor — but the moment you declare ",[48,266,267],{},"any","\nconstructor, that freebie disappears.",[72,270,272],{"className":74,"code":271,"language":76,"meta":77,"style":77},"class User {\n  String name;\n  int age;\n  User() { this(\"anon\", 0); }            \u002F\u002F no-arg, delegates\n  User(String name, int age) {           \u002F\u002F primary constructor\n    this.name = name;\n    this.age = age;\n  }\n}\n",[28,273,274,283,288,296,324,347,360,371,375],{"__ignoreMap":77},[81,275,276,278,281],{"class":83,"line":84},[81,277,45],{"class":87},[81,279,280],{"class":90}," User",[81,282,95],{"class":94},[81,284,285],{"class":83,"line":98},[81,286,287],{"class":94},"  String name;\n",[81,289,290,293],{"class":83,"line":108},[81,291,292],{"class":87},"  int",[81,294,295],{"class":94}," age;\n",[81,297,298,301,304,308,310,313,315,318,321],{"class":83,"line":130},[81,299,300],{"class":90},"  User",[81,302,303],{"class":94},"() { ",[81,305,307],{"class":306},"sj4cs","this",[81,309,206],{"class":94},[81,311,312],{"class":209},"\"anon\"",[81,314,249],{"class":94},[81,316,317],{"class":306},"0",[81,319,320],{"class":94},"); }            ",[81,322,323],{"class":104},"\u002F\u002F no-arg, delegates\n",[81,325,326,328,330,333,335,338,341,344],{"class":83,"line":142},[81,327,300],{"class":90},[81,329,117],{"class":94},[81,331,332],{"class":120},"name",[81,334,249],{"class":94},[81,336,337],{"class":87},"int",[81,339,340],{"class":120}," age",[81,342,343],{"class":94},") {           ",[81,345,346],{"class":104},"\u002F\u002F primary constructor\n",[81,348,349,352,355,357],{"class":83,"line":148},[81,350,351],{"class":306},"    this",[81,353,354],{"class":94},".name ",[81,356,136],{"class":87},[81,358,359],{"class":94}," name;\n",[81,361,362,364,367,369],{"class":83,"line":154},[81,363,351],{"class":306},[81,365,366],{"class":94},".age ",[81,368,136],{"class":87},[81,370,295],{"class":94},[81,372,373],{"class":83,"line":161},[81,374,145],{"class":94},[81,376,377],{"class":83,"line":180},[81,378,151],{"class":94},[15,380,381,382,385,386,389,390,249,393,395,396,399,400,403,404,407],{},"Constructors can be ",[19,383,384],{},"overloaded"," (several signatures), they're ",[19,387,388],{},"not inherited",", and\nthey can't be ",[28,391,392],{},"final",[28,394,34],{},", or ",[28,397,398],{},"abstract",". A constructor ",[48,401,402],{},"named"," like the class but\nwith a return type is just a method — a classic trick question. ",[19,405,406],{},"Rule of thumb:"," no\nreturn type + class name = constructor.",[10,409,411],{"id":410},"constructor-chaining-with-this-and-super","Constructor chaining with this() and super()",[15,413,414,415,418,419,422,423,426,427,430,431,434,435,438,439,442],{},"Real classes often have several constructors. Rather than duplicate setup, you ",[19,416,417],{},"chain","\nthem: ",[28,420,421],{},"this(...)"," delegates to another constructor in the ",[48,424,425],{},"same"," class, and ",[28,428,429],{},"super(...)","\ncalls a constructor in the ",[48,432,433],{},"parent"," class. Both must be the ",[19,436,437],{},"first statement"," in the\nconstructor body — and since there's only one first statement, you use ",[19,440,441],{},"at most one"," of\nthem.",[72,444,446],{"className":74,"code":445,"language":76,"meta":77,"style":77},"class Base {\n  Base(int n) { \u002F* parent setup *\u002F }\n}\nclass Sub extends Base {\n  Sub() { this(5); }            \u002F\u002F delegate within Sub...\n  Sub(int n) { super(n); }      \u002F\u002F ...which then reaches the parent\n}\n",[28,447,448,457,478,482,496,515,536],{"__ignoreMap":77},[81,449,450,452,455],{"class":83,"line":84},[81,451,45],{"class":87},[81,453,454],{"class":90}," Base",[81,456,95],{"class":94},[81,458,459,462,464,466,469,472,475],{"class":83,"line":98},[81,460,461],{"class":90},"  Base",[81,463,206],{"class":94},[81,465,337],{"class":87},[81,467,468],{"class":120}," n",[81,470,471],{"class":94},") { ",[81,473,474],{"class":104},"\u002F* parent setup *\u002F",[81,476,477],{"class":94}," }\n",[81,479,480],{"class":83,"line":108},[81,481,151],{"class":94},[81,483,484,486,489,492,494],{"class":83,"line":130},[81,485,45],{"class":87},[81,487,488],{"class":90}," Sub",[81,490,491],{"class":87}," extends",[81,493,454],{"class":90},[81,495,95],{"class":94},[81,497,498,501,503,505,507,510,512],{"class":83,"line":142},[81,499,500],{"class":90},"  Sub",[81,502,303],{"class":94},[81,504,307],{"class":306},[81,506,206],{"class":94},[81,508,509],{"class":306},"5",[81,511,320],{"class":94},[81,513,514],{"class":104},"\u002F\u002F delegate within Sub...\n",[81,516,517,519,521,523,525,527,530,533],{"class":83,"line":148},[81,518,500],{"class":90},[81,520,206],{"class":94},[81,522,337],{"class":87},[81,524,468],{"class":120},[81,526,471],{"class":94},[81,528,529],{"class":306},"super",[81,531,532],{"class":94},"(n); }      ",[81,534,535],{"class":104},"\u002F\u002F ...which then reaches the parent\n",[81,537,538],{"class":83,"line":154},[81,539,151],{"class":94},[15,541,542,543,545,546,549,550,552,553,555],{},"The idiom is to funnel every overloaded constructor through ",[28,544,421],{}," down to one\n",[19,547,548],{},"primary constructor"," that finally calls ",[28,551,429],{},". If you write no explicit\n",[28,554,429],{},", the compiler inserts a call to the parent's no-arg constructor — which fails\nto compile if the parent has none. Centralizing validation in the primary constructor means\nyou write the checks once.",[10,557,559],{"id":558},"this-vs-super","this vs super",[15,561,562,564,565,568,569,571,572,574,575,577],{},[28,563,307],{}," is a reference to the ",[19,566,567],{},"current object",". You use it to disambiguate a field from a\nsame-named parameter, to chain constructors via ",[28,570,421],{},", and to pass the current\ninstance to other methods. ",[28,573,529],{}," refers to the ",[19,576,433],{}," portion of the object — handy\nfor calling a parent constructor or a parent method you've overridden.",[72,579,581],{"className":74,"code":580,"language":76,"meta":77,"style":77},"class Point {\n  int x, y;\n  Point(int x, int y) {\n    this.x = x;            \u002F\u002F field vs parameter\n    this.y = y;\n  }\n  Point() { this(0, 0); }  \u002F\u002F constructor chaining\n}\n",[28,582,583,592,599,621,636,648,652,674],{"__ignoreMap":77},[81,584,585,587,590],{"class":83,"line":84},[81,586,45],{"class":87},[81,588,589],{"class":90}," Point",[81,591,95],{"class":94},[81,593,594,596],{"class":83,"line":98},[81,595,292],{"class":87},[81,597,598],{"class":94}," x, y;\n",[81,600,601,604,606,608,611,613,615,618],{"class":83,"line":108},[81,602,603],{"class":90},"  Point",[81,605,206],{"class":94},[81,607,337],{"class":87},[81,609,610],{"class":120}," x",[81,612,249],{"class":94},[81,614,337],{"class":87},[81,616,617],{"class":120}," y",[81,619,620],{"class":94},") {\n",[81,622,623,625,628,630,633],{"class":83,"line":130},[81,624,351],{"class":306},[81,626,627],{"class":94},".x ",[81,629,136],{"class":87},[81,631,632],{"class":94}," x;            ",[81,634,635],{"class":104},"\u002F\u002F field vs parameter\n",[81,637,638,640,643,645],{"class":83,"line":142},[81,639,351],{"class":306},[81,641,642],{"class":94},".y ",[81,644,136],{"class":87},[81,646,647],{"class":94}," y;\n",[81,649,650],{"class":83,"line":148},[81,651,145],{"class":94},[81,653,654,656,658,660,662,664,666,668,671],{"class":83,"line":154},[81,655,603],{"class":90},[81,657,303],{"class":94},[81,659,307],{"class":306},[81,661,206],{"class":94},[81,663,317],{"class":306},[81,665,249],{"class":94},[81,667,317],{"class":306},[81,669,670],{"class":94},"); }  ",[81,672,673],{"class":104},"\u002F\u002F constructor chaining\n",[81,675,676],{"class":83,"line":161},[81,677,151],{"class":94},[15,679,680,681,683,684,686,687,689,690,692],{},"The two are mirror images: ",[28,682,307],{}," looks at ",[48,685,307],{}," object's own members, ",[28,688,529],{}," reaches one\nlevel up the hierarchy. Within classes-and-objects work you'll mostly meet ",[28,691,307],{}," for field\ndisambiguation and constructor delegation.",[10,694,696],{"id":695},"static-vs-instance-members","Static vs instance members",[15,698,699,700,703,704,706,707,709,710,712,713,718],{},"By default a field or method is an ",[19,701,702],{},"instance member"," — every object gets its own copy,\naccessed through a reference. The ",[28,705,34],{}," keyword binds a member to the ",[19,708,45],{}," itself:\none shared copy for all instances, accessed through the class name. A ",[28,711,34],{}," method has\n",[19,714,715,716],{},"no ",[28,717,307],{},", so it can't touch instance fields directly — it's for utilities, factories,\nand constants.",[72,720,722],{"className":74,"code":721,"language":76,"meta":77,"style":77},"class Counter {\n  static int total;             \u002F\u002F one shared copy across all Counters\n  int id;                       \u002F\u002F unique per object\n  Counter() { id = ++total; }   \u002F\u002F bumps the shared count\n}\n\nnew Counter();                  \u002F\u002F id = 1\nnew Counter();                  \u002F\u002F id = 2\nint howMany = Counter.total;    \u002F\u002F 2 — read via the class name\n",[28,723,724,733,747,757,776,780,784,796,807],{"__ignoreMap":77},[81,725,726,728,731],{"class":83,"line":84},[81,727,45],{"class":87},[81,729,730],{"class":90}," Counter",[81,732,95],{"class":94},[81,734,735,738,741,744],{"class":83,"line":98},[81,736,737],{"class":87},"  static",[81,739,740],{"class":87}," int",[81,742,743],{"class":94}," total;             ",[81,745,746],{"class":104},"\u002F\u002F one shared copy across all Counters\n",[81,748,749,751,754],{"class":83,"line":108},[81,750,292],{"class":87},[81,752,753],{"class":94}," id;                       ",[81,755,756],{"class":104},"\u002F\u002F unique per object\n",[81,758,759,762,765,767,770,773],{"class":83,"line":130},[81,760,761],{"class":90},"  Counter",[81,763,764],{"class":94},"() { id ",[81,766,136],{"class":87},[81,768,769],{"class":87}," ++",[81,771,772],{"class":94},"total; }   ",[81,774,775],{"class":104},"\u002F\u002F bumps the shared count\n",[81,777,778],{"class":83,"line":142},[81,779,151],{"class":94},[81,781,782],{"class":83,"line":148},[81,783,158],{"emptyLinePlaceholder":157},[81,785,786,788,790,793],{"class":83,"line":154},[81,787,30],{"class":87},[81,789,730],{"class":90},[81,791,792],{"class":94},"();                  ",[81,794,795],{"class":104},"\u002F\u002F id = 1\n",[81,797,798,800,802,804],{"class":83,"line":161},[81,799,30],{"class":87},[81,801,730],{"class":90},[81,803,792],{"class":94},[81,805,806],{"class":104},"\u002F\u002F id = 2\n",[81,808,809,811,814,816,819],{"class":83,"line":180},[81,810,337],{"class":87},[81,812,813],{"class":94}," howMany ",[81,815,136],{"class":87},[81,817,818],{"class":94}," Counter.total;    ",[81,820,821],{"class":104},"\u002F\u002F 2 — read via the class name\n",[15,823,824,826,827,830,831,833,834,837],{},[19,825,406],{}," if a value or behavior is about ",[48,828,829],{},"the type"," — a constant, a factory, a\nrunning count — make it ",[28,832,34],{},"; if it's about ",[48,835,836],{},"one object's"," state, keep it an instance\nmember. Because static methods aren't tied to an object, they participate in no runtime\npolymorphism.",[10,839,841],{"id":840},"final-assign-once-and-locked-down","final: assign-once and locked-down",[15,843,844,846,847,852,853,858,859,864,865,868,869,872,873,875],{},[28,845,392],{}," means \"cannot change,\" but what it locks depends on where it sits. A ",[19,848,849,851],{},[28,850,392],{},"\nfield"," is assign-once and must be set by the end of construction — ideal for immutable\nstate. A ",[19,854,855,857],{},[28,856,392],{}," method"," can't be overridden by subclasses, and a ",[19,860,861,863],{},[28,862,392],{}," class","\ncan't be extended at all (",[28,866,867],{},"String"," and ",[28,870,871],{},"Integer"," are both ",[28,874,392],{},").",[72,877,879],{"className":74,"code":878,"language":76,"meta":77,"style":77},"class Account {\n  final long id;                       \u002F\u002F must be set during construction\n  Account(long id) { this.id = id; }   \u002F\u002F assigned exactly once\n}\n\nfinal class Constants { }              \u002F\u002F no subclass can extend this\n",[28,880,881,890,903,931,935,939],{"__ignoreMap":77},[81,882,883,885,888],{"class":83,"line":84},[81,884,45],{"class":87},[81,886,887],{"class":90}," Account",[81,889,95],{"class":94},[81,891,892,895,898,900],{"class":83,"line":98},[81,893,894],{"class":87},"  final",[81,896,897],{"class":87}," long",[81,899,753],{"class":94},[81,901,902],{"class":104},"\u002F\u002F must be set during construction\n",[81,904,905,908,910,913,916,918,920,923,925,928],{"class":83,"line":108},[81,906,907],{"class":90},"  Account",[81,909,206],{"class":94},[81,911,912],{"class":87},"long",[81,914,915],{"class":120}," id",[81,917,471],{"class":94},[81,919,307],{"class":306},[81,921,922],{"class":94},".id ",[81,924,136],{"class":87},[81,926,927],{"class":94}," id; }   ",[81,929,930],{"class":104},"\u002F\u002F assigned exactly once\n",[81,932,933],{"class":83,"line":130},[81,934,151],{"class":94},[81,936,937],{"class":83,"line":142},[81,938,158],{"emptyLinePlaceholder":157},[81,940,941,943,945,948,951],{"class":83,"line":148},[81,942,392],{"class":87},[81,944,863],{"class":87},[81,946,947],{"class":90}," Constants",[81,949,950],{"class":94}," { }              ",[81,952,953],{"class":104},"\u002F\u002F no subclass can extend this\n",[15,955,956,957,959,960,963,964,967,968,970],{},"A subtle point: ",[28,958,392],{}," on a ",[48,961,962],{},"reference"," fixes the reference, not the object it points to —\na ",[28,965,966],{},"final List"," can still have elements added. Marking genuinely-constant fields ",[28,969,392],{},"\ndocuments intent, prevents accidental reassignment, and is the backbone of immutable\ndesigns.",[10,972,974],{"id":973},"access-modifiers-controlling-visibility","Access modifiers: controlling visibility",[15,976,977,978,981,982,985,986,989,990,993,994,997],{},"Java has four levels of access, from most to least restrictive: ",[28,979,980],{},"private"," (same class\nonly), ",[19,983,984],{},"package-private"," (the default — no keyword — same package only), ",[28,987,988],{},"protected","\n(same package ",[48,991,992],{},"plus"," subclasses anywhere), and ",[28,995,996],{},"public"," (everywhere). Choosing the tightest\nlevel that works keeps coupling low and gives you freedom to refactor internals.",[72,999,1001],{"className":74,"code":1000,"language":76,"meta":77,"style":77},"public class Api {\n  private int secret;        \u002F\u002F this class only\n  int packageScoped;         \u002F\u002F same package (default)\n  protected int forSubs;     \u002F\u002F package + subclasses\n  public int open;           \u002F\u002F everyone\n}\n",[28,1002,1003,1014,1027,1037,1050,1063],{"__ignoreMap":77},[81,1004,1005,1007,1009,1012],{"class":83,"line":84},[81,1006,996],{"class":87},[81,1008,863],{"class":87},[81,1010,1011],{"class":90}," Api",[81,1013,95],{"class":94},[81,1015,1016,1019,1021,1024],{"class":83,"line":98},[81,1017,1018],{"class":87},"  private",[81,1020,740],{"class":87},[81,1022,1023],{"class":94}," secret;        ",[81,1025,1026],{"class":104},"\u002F\u002F this class only\n",[81,1028,1029,1031,1034],{"class":83,"line":108},[81,1030,292],{"class":87},[81,1032,1033],{"class":94}," packageScoped;         ",[81,1035,1036],{"class":104},"\u002F\u002F same package (default)\n",[81,1038,1039,1042,1044,1047],{"class":83,"line":130},[81,1040,1041],{"class":87},"  protected",[81,1043,740],{"class":87},[81,1045,1046],{"class":94}," forSubs;     ",[81,1048,1049],{"class":104},"\u002F\u002F package + subclasses\n",[81,1051,1052,1055,1057,1060],{"class":83,"line":142},[81,1053,1054],{"class":87},"  public",[81,1056,740],{"class":87},[81,1058,1059],{"class":94}," open;           ",[81,1061,1062],{"class":104},"\u002F\u002F everyone\n",[81,1064,1065],{"class":83,"line":148},[81,1066,151],{"class":94},[15,1068,1069,1070,1072,1073,1075,1076,1079],{},"Default to ",[28,1071,980],{}," fields and widen only when a real caller needs access. A common\nsurprise: ",[28,1074,988],{}," also grants ",[19,1077,1078],{},"package"," access, so it's broader than \"subclasses\nonly.\" Encapsulating fields behind methods lets you add validation or change representation\nwithout breaking callers.",[10,1081,1083],{"id":1082},"initializer-blocks","Initializer blocks",[15,1085,1086,1087,1090,1091,1094,1095,1098,1099,1090,1102,1105,1106,1111,1112,1115],{},"Most setup belongs in field initializers or constructors, but sometimes initialization\nneeds real logic — a loop, a try\u002Fcatch — that a single expression can't express. Java\noffers two block forms. A ",[19,1088,1089],{},"static initializer block"," (",[28,1092,1093],{},"static { ... }",") runs ",[19,1096,1097],{},"once","\nwhen the class first loads; an ",[19,1100,1101],{},"instance initializer block",[28,1103,1104],{},"{ ... }",") runs on ",[19,1107,1108,1109],{},"every\n",[28,1110,30],{},", after ",[28,1113,1114],{},"super()"," and before the constructor body, sharing setup across overloaded\nconstructors.",[72,1117,1119],{"className":74,"code":1118,"language":76,"meta":77,"style":77},"class Config {\n  static final Map\u003CString, String> DEFAULTS;\n  static {                                   \u002F\u002F runs once at class load\n    DEFAULTS = new HashMap\u003C>();\n    DEFAULTS.put(\"env\", \"dev\");\n  }\n  final long created;\n  { created = System.currentTimeMillis(); }  \u002F\u002F runs for each instance\n}\n",[28,1120,1121,1130,1149,1159,1171,1192,1196,1205,1224],{"__ignoreMap":77},[81,1122,1123,1125,1128],{"class":83,"line":84},[81,1124,45],{"class":87},[81,1126,1127],{"class":90}," Config",[81,1129,95],{"class":94},[81,1131,1132,1134,1137,1140,1142,1144,1146],{"class":83,"line":98},[81,1133,737],{"class":87},[81,1135,1136],{"class":87}," final",[81,1138,1139],{"class":94}," Map\u003C",[81,1141,867],{"class":87},[81,1143,249],{"class":94},[81,1145,867],{"class":87},[81,1147,1148],{"class":94},"> DEFAULTS;\n",[81,1150,1151,1153,1156],{"class":83,"line":108},[81,1152,737],{"class":87},[81,1154,1155],{"class":94}," {                                   ",[81,1157,1158],{"class":104},"\u002F\u002F runs once at class load\n",[81,1160,1161,1164,1166,1168],{"class":83,"line":130},[81,1162,1163],{"class":94},"    DEFAULTS ",[81,1165,136],{"class":87},[81,1167,169],{"class":87},[81,1169,1170],{"class":94}," HashMap\u003C>();\n",[81,1172,1173,1176,1179,1181,1184,1186,1189],{"class":83,"line":142},[81,1174,1175],{"class":94},"    DEFAULTS.",[81,1177,1178],{"class":90},"put",[81,1180,206],{"class":94},[81,1182,1183],{"class":209},"\"env\"",[81,1185,249],{"class":94},[81,1187,1188],{"class":209},"\"dev\"",[81,1190,1191],{"class":94},");\n",[81,1193,1194],{"class":83,"line":148},[81,1195,145],{"class":94},[81,1197,1198,1200,1202],{"class":83,"line":154},[81,1199,894],{"class":87},[81,1201,897],{"class":87},[81,1203,1204],{"class":94}," created;\n",[81,1206,1207,1210,1212,1215,1218,1221],{"class":83,"line":161},[81,1208,1209],{"class":94},"  { created ",[81,1211,136],{"class":87},[81,1213,1214],{"class":94}," System.",[81,1216,1217],{"class":90},"currentTimeMillis",[81,1219,1220],{"class":94},"(); }  ",[81,1222,1223],{"class":104},"\u002F\u002F runs for each instance\n",[81,1225,1226],{"class":83,"line":180},[81,1227,151],{"class":94},[15,1229,1230,1232],{},[19,1231,406],{}," prefer field initializers or constructors; reach for blocks only when\nthe initialization genuinely needs statements. Knowing the run order — static block once,\nthen per-instance the block before the constructor body — is a frequent interview probe.",[10,1234,1236],{"id":1235},"creating-objects-new-and-the-heap","Creating objects: new and the heap",[15,1238,1239,1240,1242,1243,1246,1247,1250],{},"The normal way to make an object is ",[28,1241,30],{},", which ",[19,1244,1245],{},"allocates memory on the heap",", runs the\nmatching constructor, and returns a reference you store in a variable (which lives on the\nstack). The reference is how you reach the object; the object itself stays on the heap until\nno references remain, at which point the ",[19,1248,1249],{},"garbage collector"," reclaims it.",[72,1252,1254],{"className":74,"code":1253,"language":76,"meta":77,"style":77},"User a = new User();          \u002F\u002F allocates on the heap, runs constructor\nUser b = a;                   \u002F\u002F copies the reference, not the object\nb.name = \"Bo\";                \u002F\u002F a.name is now \"Bo\" too — same object\n",[28,1255,1256,1273,1286],{"__ignoreMap":77},[81,1257,1258,1261,1263,1265,1267,1270],{"class":83,"line":84},[81,1259,1260],{"class":94},"User a ",[81,1262,136],{"class":87},[81,1264,169],{"class":87},[81,1266,280],{"class":90},[81,1268,1269],{"class":94},"();          ",[81,1271,1272],{"class":104},"\u002F\u002F allocates on the heap, runs constructor\n",[81,1274,1275,1278,1280,1283],{"class":83,"line":98},[81,1276,1277],{"class":94},"User b ",[81,1279,136],{"class":87},[81,1281,1282],{"class":94}," a;                   ",[81,1284,1285],{"class":104},"\u002F\u002F copies the reference, not the object\n",[81,1287,1288,1291,1293,1296,1299],{"class":83,"line":108},[81,1289,1290],{"class":94},"b.name ",[81,1292,136],{"class":87},[81,1294,1295],{"class":209}," \"Bo\"",[81,1297,1298],{"class":94},";                ",[81,1300,1301],{"class":104},"\u002F\u002F a.name is now \"Bo\" too — same object\n",[15,1303,1304,1305,1307,1308,1090,1311,1314,1315,1318,1319,1322,1323,1325],{},"Beyond ",[28,1306,30],{},", objects can also be made via ",[19,1309,1310],{},"factory methods",[28,1312,1313],{},"List.of(...)","), the\n",[19,1316,1317],{},"reflection"," API, ",[28,1320,1321],{},"clone()",", and deserialization — and the last two can bypass\nconstructors entirely, which is why immutable types and singletons sometimes need extra\nguards. For everyday code, ",[28,1324,30],{}," and factory methods are what you reach for.",[10,1327,1329],{"id":1328},"recap","Recap",[15,1331,42,1332,1334,1335,1337,1338,1341,1342,1344,1345,1347,1348,1350,1351,1353,1354,1356,1357,1359,1360,1362,1363,1365,1366,1368,1369,1372,1373,1375,1376,1379,1380,1382],{},[19,1333,45],{}," is the blueprint; an ",[19,1336,62],{}," is a heap-resident instance with its own copy\nof the instance fields. ",[19,1339,1340],{},"Constructors"," initialize objects — same name as the class, no\nreturn type — and you ",[19,1343,417],{}," them with ",[28,1346,421],{}," (same class) and ",[28,1349,429],{},"\n(parent), each required to be the first statement so only one appears. ",[28,1352,307],{}," references the\ncurrent object, ",[28,1355,529],{}," the parent portion. ",[28,1358,34],{}," members belong to the ",[19,1361,45],{}," (one\nshared copy, no ",[28,1364,307],{},"); instance members belong to each object. ",[28,1367,392],{}," locks fields,\nmethods, or whole classes; the four ",[19,1370,1371],{},"access modifiers"," control visibility, with ",[28,1374,980],{},"\nas the sensible default. Reach for ",[19,1377,1378],{},"initializer blocks"," only when setup needs real logic,\nand remember that ",[28,1381,30],{}," allocates on the heap and hands back a reference. Master these\nfundamentals and the harder OOP topics — inheritance, polymorphism, immutability — all\nbuild cleanly on top.",[1384,1385,1386],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":77,"searchDepth":98,"depth":98,"links":1388},[1389,1390,1391,1392,1393,1394,1395,1396,1397,1398,1399],{"id":12,"depth":98,"text":13},{"id":38,"depth":98,"text":39},{"id":238,"depth":98,"text":239},{"id":410,"depth":98,"text":411},{"id":558,"depth":98,"text":559},{"id":695,"depth":98,"text":696},{"id":840,"depth":98,"text":841},{"id":973,"depth":98,"text":974},{"id":1082,"depth":98,"text":1083},{"id":1235,"depth":98,"text":1236},{"id":1328,"depth":98,"text":1329},"Java classes and objects explained — fields and methods, constructors and chaining with this()\u002Fsuper(), this vs super, static vs instance members, final, access modifiers, initializer blocks, and how new allocates on the heap.","easy","md","Java",{},"\u002Fblog\u002Fjava-classes-objects-constructors","\u002Fjava\u002Foop\u002Fclasses-objects",{"title":5,"description":1400},"blog\u002Fjava-classes-objects-constructors","Classes & Objects","Object-Oriented Programming","oop","2026-06-20","EuDNx2EIdzFUNJcBV3cms-8iw-PYNTpt7RVK1Cdd4T4",1782244089365]