When did add() method add object in collection
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I have actually 2 questions.
QUESTION 1
Consider the below code.
public class Test
private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("INNER : "+var);
;
public static void main(String... args)
System.out.println("OUTER : "+var);
When i run this code it gives me below output
INNER : null
OUTER : [A, B]
Can any one elaborate why INNER
is null and execution flow at a time when exactly "A" & "B" added to collection
.
QUESTION 2
I made some changes in above code and modified to below one(just put add method within first bracket)
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var); return true;
;
;
public static void main(String... args)
System.out.println("OUTER : "+var);
After running above code i got below result
OUTER :
After viewing this I'm totally clueless here about what is going on ... where did INNER gone ??? why it's not printing ? is it not called ?
Great help if anyone can explain this behavior and fundamental behind it.
java arraylist collections
add a comment |Â
up vote
7
down vote
favorite
I have actually 2 questions.
QUESTION 1
Consider the below code.
public class Test
private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("INNER : "+var);
;
public static void main(String... args)
System.out.println("OUTER : "+var);
When i run this code it gives me below output
INNER : null
OUTER : [A, B]
Can any one elaborate why INNER
is null and execution flow at a time when exactly "A" & "B" added to collection
.
QUESTION 2
I made some changes in above code and modified to below one(just put add method within first bracket)
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var); return true;
;
;
public static void main(String... args)
System.out.println("OUTER : "+var);
After running above code i got below result
OUTER :
After viewing this I'm totally clueless here about what is going on ... where did INNER gone ??? why it's not printing ? is it not called ?
Great help if anyone can explain this behavior and fundamental behind it.
java arraylist collections
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I have actually 2 questions.
QUESTION 1
Consider the below code.
public class Test
private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("INNER : "+var);
;
public static void main(String... args)
System.out.println("OUTER : "+var);
When i run this code it gives me below output
INNER : null
OUTER : [A, B]
Can any one elaborate why INNER
is null and execution flow at a time when exactly "A" & "B" added to collection
.
QUESTION 2
I made some changes in above code and modified to below one(just put add method within first bracket)
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var); return true;
;
;
public static void main(String... args)
System.out.println("OUTER : "+var);
After running above code i got below result
OUTER :
After viewing this I'm totally clueless here about what is going on ... where did INNER gone ??? why it's not printing ? is it not called ?
Great help if anyone can explain this behavior and fundamental behind it.
java arraylist collections
I have actually 2 questions.
QUESTION 1
Consider the below code.
public class Test
private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("INNER : "+var);
;
public static void main(String... args)
System.out.println("OUTER : "+var);
When i run this code it gives me below output
INNER : null
OUTER : [A, B]
Can any one elaborate why INNER
is null and execution flow at a time when exactly "A" & "B" added to collection
.
QUESTION 2
I made some changes in above code and modified to below one(just put add method within first bracket)
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var); return true;
;
;
public static void main(String... args)
System.out.println("OUTER : "+var);
After running above code i got below result
OUTER :
After viewing this I'm totally clueless here about what is going on ... where did INNER gone ??? why it's not printing ? is it not called ?
Great help if anyone can explain this behavior and fundamental behind it.
java arraylist collections
java arraylist collections
edited 51 mins ago


maspinu
7032922
7032922
asked 56 mins ago
RBS
434
434
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
5
down vote
accepted
Because the initializer block of the anonymous class off
ArrayList
is executed before the instance reference of the anonymous class is assigned tovar
.The code is not executed because you never call the
add(String e)
method. If you did, it would result in aStackOverflowError
, sinceadd("A")
is a recursive call now.
if i call add() from main method how does it becomes recursive call(i check it as you mentioned it gives StackOverflowError)
– RBS
32 mins ago
2
@RBS The first statement insideadd(String e)
isadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, and StackOverflowError.
– Andreas
25 mins ago
add a comment |Â
up vote
1
down vote
In your code snippet 1, you are using the double brace initialization to add items to the array list. It is basically an anonymous inner class with an instance initializer. Since you print var
before the object is constructed it is null
.
In your second snippet, you are creating an anonymous class of ArrayList providing an implementation of the add
method. But no one calls the add
method on the ArrayList object.
EDIT: Good point made by Andreas@ - Even if you call the add method, it will result in an infinite recursive call resulting in a StackOverflowError
.
Reference:
Initialization of an ArrayList in one line
add a comment |Â
up vote
1
down vote
The object you created has not been assigned to
var
in the initialization procedure, try this:private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("THIS : " + this); // THIS : [A, B]
System.out.println("INNER : " + var); // INNER : null
;You just override
add
method ofArrayList
, it's nerver been called. Which basicly equals:static class CustomList<E> extends ArrayList<E>
@Override
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var);
return true;
private static final List<String> var = new CustomList<>();
add a comment |Â
up vote
0
down vote
In the first code you print the variable before that its constructor has returned as you add the elements and print the list inside the instance initializer.
So it can only print null
.
In the second code you override add()
to add hardcoded elements but add()
is never invoked on the created instance. So add()
doesn't print anything and nothing is added in the List instance either.
To add elements in a List you generally want to invoke add()
on the List reference such as :
List<String> list = new ArrayList<>();
list.add("element A");
list.add("element B");
You don't create an anonymous class for such a requirement.
If you want to use a simpler syntax you can still do :
List<String> list = new ArrayList<>(Arrays.asList("element A", "element B"));
add a comment |Â
up vote
-1
down vote
In you first code var is null because the return value of new operator is not assigned to var.
In the second code you override the add method and it is never called, to call that run this code:
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
/*add("A");
add("B");*/
super.add(e);
System.out.println("INNER : " + var); return true;
;
add("A");
add("B");
;
public static void main(String... args)
System.out.println("OUTER : "+var);
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Because the initializer block of the anonymous class off
ArrayList
is executed before the instance reference of the anonymous class is assigned tovar
.The code is not executed because you never call the
add(String e)
method. If you did, it would result in aStackOverflowError
, sinceadd("A")
is a recursive call now.
if i call add() from main method how does it becomes recursive call(i check it as you mentioned it gives StackOverflowError)
– RBS
32 mins ago
2
@RBS The first statement insideadd(String e)
isadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, and StackOverflowError.
– Andreas
25 mins ago
add a comment |Â
up vote
5
down vote
accepted
Because the initializer block of the anonymous class off
ArrayList
is executed before the instance reference of the anonymous class is assigned tovar
.The code is not executed because you never call the
add(String e)
method. If you did, it would result in aStackOverflowError
, sinceadd("A")
is a recursive call now.
if i call add() from main method how does it becomes recursive call(i check it as you mentioned it gives StackOverflowError)
– RBS
32 mins ago
2
@RBS The first statement insideadd(String e)
isadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, and StackOverflowError.
– Andreas
25 mins ago
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Because the initializer block of the anonymous class off
ArrayList
is executed before the instance reference of the anonymous class is assigned tovar
.The code is not executed because you never call the
add(String e)
method. If you did, it would result in aStackOverflowError
, sinceadd("A")
is a recursive call now.
Because the initializer block of the anonymous class off
ArrayList
is executed before the instance reference of the anonymous class is assigned tovar
.The code is not executed because you never call the
add(String e)
method. If you did, it would result in aStackOverflowError
, sinceadd("A")
is a recursive call now.
answered 49 mins ago


Andreas
69.5k450107
69.5k450107
if i call add() from main method how does it becomes recursive call(i check it as you mentioned it gives StackOverflowError)
– RBS
32 mins ago
2
@RBS The first statement insideadd(String e)
isadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, and StackOverflowError.
– Andreas
25 mins ago
add a comment |Â
if i call add() from main method how does it becomes recursive call(i check it as you mentioned it gives StackOverflowError)
– RBS
32 mins ago
2
@RBS The first statement insideadd(String e)
isadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, which is a call to itself, so it executesadd("A")
, and StackOverflowError.
– Andreas
25 mins ago
if i call add() from main method how does it becomes recursive call(i check it as you mentioned it gives StackOverflowError)
– RBS
32 mins ago
if i call add() from main method how does it becomes recursive call(i check it as you mentioned it gives StackOverflowError)
– RBS
32 mins ago
2
2
@RBS The first statement inside
add(String e)
is add("A")
, which is a call to itself, so it executes add("A")
, which is a call to itself, so it executes add("A")
, which is a call to itself, so it executes add("A")
, which is a call to itself, so it executes add("A")
, which is a call to itself, so it executes add("A")
, and StackOverflowError.– Andreas
25 mins ago
@RBS The first statement inside
add(String e)
is add("A")
, which is a call to itself, so it executes add("A")
, which is a call to itself, so it executes add("A")
, which is a call to itself, so it executes add("A")
, which is a call to itself, so it executes add("A")
, which is a call to itself, so it executes add("A")
, and StackOverflowError.– Andreas
25 mins ago
add a comment |Â
up vote
1
down vote
In your code snippet 1, you are using the double brace initialization to add items to the array list. It is basically an anonymous inner class with an instance initializer. Since you print var
before the object is constructed it is null
.
In your second snippet, you are creating an anonymous class of ArrayList providing an implementation of the add
method. But no one calls the add
method on the ArrayList object.
EDIT: Good point made by Andreas@ - Even if you call the add method, it will result in an infinite recursive call resulting in a StackOverflowError
.
Reference:
Initialization of an ArrayList in one line
add a comment |Â
up vote
1
down vote
In your code snippet 1, you are using the double brace initialization to add items to the array list. It is basically an anonymous inner class with an instance initializer. Since you print var
before the object is constructed it is null
.
In your second snippet, you are creating an anonymous class of ArrayList providing an implementation of the add
method. But no one calls the add
method on the ArrayList object.
EDIT: Good point made by Andreas@ - Even if you call the add method, it will result in an infinite recursive call resulting in a StackOverflowError
.
Reference:
Initialization of an ArrayList in one line
add a comment |Â
up vote
1
down vote
up vote
1
down vote
In your code snippet 1, you are using the double brace initialization to add items to the array list. It is basically an anonymous inner class with an instance initializer. Since you print var
before the object is constructed it is null
.
In your second snippet, you are creating an anonymous class of ArrayList providing an implementation of the add
method. But no one calls the add
method on the ArrayList object.
EDIT: Good point made by Andreas@ - Even if you call the add method, it will result in an infinite recursive call resulting in a StackOverflowError
.
Reference:
Initialization of an ArrayList in one line
In your code snippet 1, you are using the double brace initialization to add items to the array list. It is basically an anonymous inner class with an instance initializer. Since you print var
before the object is constructed it is null
.
In your second snippet, you are creating an anonymous class of ArrayList providing an implementation of the add
method. But no one calls the add
method on the ArrayList object.
EDIT: Good point made by Andreas@ - Even if you call the add method, it will result in an infinite recursive call resulting in a StackOverflowError
.
Reference:
Initialization of an ArrayList in one line
answered 49 mins ago
user7
8,12032038
8,12032038
add a comment |Â
add a comment |Â
up vote
1
down vote
The object you created has not been assigned to
var
in the initialization procedure, try this:private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("THIS : " + this); // THIS : [A, B]
System.out.println("INNER : " + var); // INNER : null
;You just override
add
method ofArrayList
, it's nerver been called. Which basicly equals:static class CustomList<E> extends ArrayList<E>
@Override
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var);
return true;
private static final List<String> var = new CustomList<>();
add a comment |Â
up vote
1
down vote
The object you created has not been assigned to
var
in the initialization procedure, try this:private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("THIS : " + this); // THIS : [A, B]
System.out.println("INNER : " + var); // INNER : null
;You just override
add
method ofArrayList
, it's nerver been called. Which basicly equals:static class CustomList<E> extends ArrayList<E>
@Override
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var);
return true;
private static final List<String> var = new CustomList<>();
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The object you created has not been assigned to
var
in the initialization procedure, try this:private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("THIS : " + this); // THIS : [A, B]
System.out.println("INNER : " + var); // INNER : null
;You just override
add
method ofArrayList
, it's nerver been called. Which basicly equals:static class CustomList<E> extends ArrayList<E>
@Override
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var);
return true;
private static final List<String> var = new CustomList<>();
The object you created has not been assigned to
var
in the initialization procedure, try this:private static final List<String> var = new ArrayList<String>()
add("A");
add("B");
System.out.println("THIS : " + this); // THIS : [A, B]
System.out.println("INNER : " + var); // INNER : null
;You just override
add
method ofArrayList
, it's nerver been called. Which basicly equals:static class CustomList<E> extends ArrayList<E>
@Override
public boolean add(String e)
add("A");
add("B");
System.out.println("INNER : " + var);
return true;
private static final List<String> var = new CustomList<>();
edited 39 mins ago
answered 47 mins ago


å™兴斌
14.8k41542
14.8k41542
add a comment |Â
add a comment |Â
up vote
0
down vote
In the first code you print the variable before that its constructor has returned as you add the elements and print the list inside the instance initializer.
So it can only print null
.
In the second code you override add()
to add hardcoded elements but add()
is never invoked on the created instance. So add()
doesn't print anything and nothing is added in the List instance either.
To add elements in a List you generally want to invoke add()
on the List reference such as :
List<String> list = new ArrayList<>();
list.add("element A");
list.add("element B");
You don't create an anonymous class for such a requirement.
If you want to use a simpler syntax you can still do :
List<String> list = new ArrayList<>(Arrays.asList("element A", "element B"));
add a comment |Â
up vote
0
down vote
In the first code you print the variable before that its constructor has returned as you add the elements and print the list inside the instance initializer.
So it can only print null
.
In the second code you override add()
to add hardcoded elements but add()
is never invoked on the created instance. So add()
doesn't print anything and nothing is added in the List instance either.
To add elements in a List you generally want to invoke add()
on the List reference such as :
List<String> list = new ArrayList<>();
list.add("element A");
list.add("element B");
You don't create an anonymous class for such a requirement.
If you want to use a simpler syntax you can still do :
List<String> list = new ArrayList<>(Arrays.asList("element A", "element B"));
add a comment |Â
up vote
0
down vote
up vote
0
down vote
In the first code you print the variable before that its constructor has returned as you add the elements and print the list inside the instance initializer.
So it can only print null
.
In the second code you override add()
to add hardcoded elements but add()
is never invoked on the created instance. So add()
doesn't print anything and nothing is added in the List instance either.
To add elements in a List you generally want to invoke add()
on the List reference such as :
List<String> list = new ArrayList<>();
list.add("element A");
list.add("element B");
You don't create an anonymous class for such a requirement.
If you want to use a simpler syntax you can still do :
List<String> list = new ArrayList<>(Arrays.asList("element A", "element B"));
In the first code you print the variable before that its constructor has returned as you add the elements and print the list inside the instance initializer.
So it can only print null
.
In the second code you override add()
to add hardcoded elements but add()
is never invoked on the created instance. So add()
doesn't print anything and nothing is added in the List instance either.
To add elements in a List you generally want to invoke add()
on the List reference such as :
List<String> list = new ArrayList<>();
list.add("element A");
list.add("element B");
You don't create an anonymous class for such a requirement.
If you want to use a simpler syntax you can still do :
List<String> list = new ArrayList<>(Arrays.asList("element A", "element B"));
edited 40 mins ago
answered 47 mins ago


davidxxx
58.2k54581
58.2k54581
add a comment |Â
add a comment |Â
up vote
-1
down vote
In you first code var is null because the return value of new operator is not assigned to var.
In the second code you override the add method and it is never called, to call that run this code:
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
/*add("A");
add("B");*/
super.add(e);
System.out.println("INNER : " + var); return true;
;
add("A");
add("B");
;
public static void main(String... args)
System.out.println("OUTER : "+var);
add a comment |Â
up vote
-1
down vote
In you first code var is null because the return value of new operator is not assigned to var.
In the second code you override the add method and it is never called, to call that run this code:
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
/*add("A");
add("B");*/
super.add(e);
System.out.println("INNER : " + var); return true;
;
add("A");
add("B");
;
public static void main(String... args)
System.out.println("OUTER : "+var);
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
In you first code var is null because the return value of new operator is not assigned to var.
In the second code you override the add method and it is never called, to call that run this code:
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
/*add("A");
add("B");*/
super.add(e);
System.out.println("INNER : " + var); return true;
;
add("A");
add("B");
;
public static void main(String... args)
System.out.println("OUTER : "+var);
In you first code var is null because the return value of new operator is not assigned to var.
In the second code you override the add method and it is never called, to call that run this code:
public class Test
private static final List<String> var = new ArrayList<String>()
public boolean add(String e)
/*add("A");
add("B");*/
super.add(e);
System.out.println("INNER : " + var); return true;
;
add("A");
add("B");
;
public static void main(String... args)
System.out.println("OUTER : "+var);
answered 40 mins ago
Rahim Dastar
707515
707515
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52689611%2fwhen-did-add-method-add-object-in-collection%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password