Static array of objects

public class Array { static String[] a = new String[] {"red", "green", "blue"}; static Point[] p = new Point[] {new Point(1, 2), "3,4"}; public static void main(String[] args) { System.out.println("hello"); } class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; } Point(String s) { String[] a = s.split(","); x = a[0].parseInt(); y = a[1].parseInt(); } } } 

In the above program, the static initialization of the Point array fails: error message:

 Array.java:4: non-static variable this cannot be referenced from a static context static Point[] p = new Point[] {new Point(1, 2), "3,4"}; 

But the static String array succeeds. What is the difference between the two?

I really need an array of static objects because it is easily referenced without creating an instance of the outer class.

thanks

+6
source share
6 answers

You need to make Point static nested class, for example:

 static class Point { 
+4
source

You need to do three things to make your code work. I will explain them. Check out the working version first.

 public class Array { static String[] a = new String[]{"red", "green", "blue"}; static Point[] p = new Point[]{new Point(1, 2), new Point("3,4")}; public static void main(String[] args) { System.out.println("hello"); } static class Point { int x; int y; Point(int x, int y) { this.x = x; this.y = y; } Point(String s) { String[] a = s.split(","); x = Integer.parseInt(a[0]); y = Integer.parseInt(a[1]); } } } 

Listed below are three changes you should make.

1. change "3,4" to new Point("3,4") or new Point(3,4)

We know that an array can contain elements of similar types. Here you declare an array named p type Point . This means that it can only contain an element of type Point (or its subtypes). But the second element of "3,4" is of type String , and you have a mismatch. Therefore, you must specify either new Point("3,4") or new Point(3,4) to get elements of type Point .

2. You must make your Point class static

From the Java Tutorial :

 An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. 

Here, your Point class is an inner class and should have access to all members of the Array class. To do this, each object of class Point must be associated with an object of class Array . But the p array you create is in a static context. Thus, you need to either make the class Point a static or make the array p non-stationary.

3. parseInt not a String class method

parseInt is a static method of the Integer class, not the String class. Therefore you should name it as Integer.parseInt(stringValue) .

Hope this helps :)

+5
source

Point[] must be an array or Point "3,4" not an instance of Point and make a static Point class for this error

+1
source

Here is an explanation from the Oracle Online Reference

Inner classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to this methods and object fields. In addition, since the inner class is associated with the instance, it cannot define any static members on its own.

Objects that are instances of an inner class exist inside an instance of an outer class. Consider the following classes:

 class OuterClass { ... class InnerClass { ... } } 

An InnerClass instance can exist only in an OuterClass instance and has direct access to the methods and fields that span the instance.

In addition, it cannot be accessed from static members of the closing type, since this requires the instance to exist.

+1
source

Declare your Point class static:

 ... static class Point { ... } 

This is necessary because Point is an inner class ( String not).

However, {new Point(1, 2), "3,4"}; - this is your next problem. I think it should be {new Point(1, 2), Point(3, 4)};

+1
source

You might want to create a Point Static cluster.

 static class Point { } 

check this

0
source

Source: https://habr.com/ru/post/909057/


All Articles