1

A strange thing happened to me which I am not able to explain to myself, so I hope someone can explain it to me. The situation is the following: I have an class called "Item". Instances of these class are instantiated with a name and two integer arrays, representing some values for this item (the meaning of these values are not important for this).

This works:

private Item item = new Item("Something", null, null);

This isn't:

private Item item = new Item("Something", {"A", "B"}, null);

This however works:

private String[] str = {"A", "B"};
private Item item = new Item("Something", str, null);

So.. my question is: Why? I absolutelty don't see why the second method isn't possible.

1 Answer 1

2

The compiler doesnt automatically know the type of the array so it has to be expressly defined when declaring it as an expression

private Item item = new Item("Something", new String[] {"A", "B"}, null);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh well, that actually does make sense! Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.