Are static constructors?

I just wrote this constructor:

public ArchivesManager(String identifier) {
    String[] components = String.split("\nNEW");
}

But there is an error: non-static method split(java.lang.String) cannot be referenced from a static context. I know this error message, but why the stator ?!

+3
source share
7 answers

This is because split must be called on a String object. I.e.

String foo = "Hello, world";
String[] arr = foo.split(",");
+9

I know this error message, but why the stator ?!

The constructor context is not static, but you explicitly called the method splitin a static context when you qualified it using the class name; those. String.split(...).

You should probably write this:

String[] components = identifier.split("\nNEW");

which calls the method in the (non-stationary) context of the object Stringpassed as identifier; that is, it indicates which line should be split.

+4

:

" " [ JLS], . , ( , ). , , void. , , , , . ( 1.4 , "external this" final .)

+3

- String.split("\nNEW"); split

, ,

public ArchivesManager(String identifier) {
    String[] components = identifier.split("\nNEW");//NOTE: components are local to const. this doesn't make sense
}
+2

String.split("\nNEW"); identifier.split("\nNEW");. ( ). , , " ", , , .

0

, . , Split , 2 thisc- ..........

0

, , . " Java" , " , ".

-1

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


All Articles