About 9,620,000 results
Open links in new tab
  1. Initializing Fields (The Java™ Tutorials > Learning the Java

    Initializing Fields As you have seen, you can often provide an initial value for a field in its declaration: public class BedAndBreakfast { // initialize to 10 public static int capacity = 10; // initialize to false private boolean full = false; }

  2. java - how to initialize a class field? - Stack Overflow

    Jun 19, 2012 · In the first approach you place the declaration and the defintion together, so you won't forget to initialze the variable. The second and thrid approaches allow more flexibility if you want to initialize differently based on some conditions.

  3. A Guide to Java Initialization - Baeldung

    Apr 17, 2025 · In this article, we covered the initialization of fields in Java. Then we examined different data types in Java and how to use them. We also explored several ways of creating objects in Java.

  4. java - Initialize class fields in constructor or at declaration ...

    Aug 24, 2008 · Assuming the type in your example, definitely prefer to initialize fields in the constructor. The exceptional cases are: Fields in static classes/methods; Fields typed as static/final/et al

  5. Class and object initialization in Java - InfoWorld

    May 24, 2024 · In this Java tutorial, you’ve learned how to use class field initializers and class initialization blocks to initialize classes, and how to use constructors, object field...

  6. java - How do I initialize classes with lots of fields in an elegant ...

    Oct 13, 2015 · A constructor other than the default constructor (the default constructor is currently Offer()) is useful for initializing the values of the fields in your class. A version of Offer using constructors would look like this:

  7. Understanding Java Object Fields: Initialization Best Practices

    In Java, object fields (also known as instance variables) are initialized during the construction of an object. Initialization can happen in several ways, including constructor initialization, default initialization, and when declared directly within the class.

  8. All you need to know about Initializing Fields in Java

    Jun 3, 2023 · There are three types of variables you can define inside a Java Class. Instance Variables. Static Variables or Class Variables. Local Variables or I call them Scoped Variables. Let’s Focus on the...

  9. All about Java’s instance initializer blocks - Oracle Blogs

    Apr 27, 2023 · Initializers can be used to set initial values for fields in objects and classes. There are three kinds of initializers: Field initializer expressions; Static initializer blocks; Instance initializer blocks; Initialization of fields can be specified in field declaration statements using initializer expressions.

  10. Java. Initialize variables in class methods. Initialization of fields ...

    Sep 14, 2018 · In Java, you can initialize a variable if it is a member of the class. There are four ways to initialize members of the class data: initialization by default (implicit initialization); explicit initialization with initial values (constant values); explicit initialization using class methods; initialization using class constructors. ⇑

Refresh