Notification texts go here Contact Us Buy Now!

Java class variable assignment throws error

public class Solution { public static void main(String[] args) { Map<Integer, Node> map = new HashMap<>(); public Node left = new Node(Integer.MIN_VALUE, null, null); public Node right = new Node(Integer.MAX_VALUE, null, null); left.next = right; right.prev = left; System.out.println("Works good"); } }
Issue: The code attempts to define fields, such as 'left' and 'right', within the class body, which is invalid in Java. To rectify this issue, the field declarations should be moved inside an appropriate context, such as a constructor or a method. Solutions:
public class SolutionWithSettingInCtor { private final Map<Integer, Node> map = new HashMap<>(); private final Node left; private final Node right; public SolutionWithSettingInCtor(Node left, Node right) { this.left = left; this.right = right; } public static void main(String[] args) { final Node left = new Node(Integer.MIN_VALUE, null, null); final Node right = new Node(Integer.MAX_VALUE, null, null); left.next = right; right.prev = left; final var solution = new SolutionWithSettingInCtor(left, right); System.out.println(left.next); System.out.println(right.prev); } }
Constructor with Parameters: In this approach, 'left' and 'right' Node instances are passed as parameters to the constructor, allowing them to be initialized. The responsibility of setting up relationships among these nodes is delegated to an external method.
public class SolutionWithFieldsSetInCtor { private final Map<Integer, Node> map = new HashMap<>(); private final Node left; private final Node right; public SolutionWithFieldsSetInCtor() { this.left = new Node(Integer.MIN_VALUE, null, null); this.right = new Node(Integer.MAX_VALUE, null, null); this.setUpNodes(); } private void setupNodes() { this.left.next = right; this.right.prev = left; } public static void main(String[] args) { final var solution = new SolutionWithSettingInCtor(); System.out.println(left.next); System.out.println(right.prev); } }
Constructor with Local Fields Set in C'tor: Here, Node instances are initialized within the constructor itself, along with the setup of relationships among these nodes.
public class SolutionWithStaticInitializerBlock { Map<Integer, Node> map = new HashMap<>(); public static Node left; public static Node right; static { left = new Node(Integer.MIN_VALUE, null, null); right = new Node(Integer.MAX_VALUE, null, null); left.next = right; right.prev = left; } public static void main(String[] args) { System.out.println(SolutionWithStaticInitializerBlock.left.next); System.out.println(SolutionWithStaticInitializerBlock.right.prev); } }
Static Initializer Blocks: This approach utilizes static fields and a static block to initialize these fields without requiring instance instantiation. However, it should be used cautiously due to potential side-effect

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.