8

Possible Duplicate:
What is Type<Type> called?
What does List<?> mean in java generics?

package com.xyz.pckgeName; import java.util.ArrayList; import java.util.List; public class Statement { // public String public String status; public String user_id; public String name; public String available_balance; public String current_balance; public String credit_card_type; public String bank_id; public List<Statements> statements = new ArrayList<Statement.Statements>(); public class Statements { public String month; public String account_id; public String user_id; public String id; public List<Transaction> transactions = new ArrayList<Transaction>(); } } 

Can anyone explain me what these two statements mean

public List<Statements> statements = new ArrayList<Statement.Statements>(); public List<Transaction> transactions = new ArrayList<Transaction>(); 
3

4 Answers 4

17

This is Generics in java

List<?> is essentially translated as a "List of unknowns", i.e., a list of unknown types. The ? is known as a Wildcard (which essentially means unknown).


public List<Statements> statements = new ArrayList<Statement.Statements>(); 

This essentially creates a List that only accepts Statement.Statements. Anything outside of Statement.Statements that you want to add to statements will create a compilation error. The same applies to public List<Transaction> transactions = new ArrayList<Transaction>();. This means that the List is bounded to a type Statement.Statements (on statements variable).

Sign up to request clarification or add additional context in comments.

2 Comments

Can we simply say that it stores objects of Statements which in nested class in Statement?'
Thanks for reply, let me read documentation and make some examples, I am upvoting and accepting answer :-)
2

It is the use of generics. You are declaring a List of either Statement objects or Transaction objects.

Check out wikipedia for more info

http://en.wikipedia.org/wiki/Generics_in_Java

Comments

2

You should read about Generics to understand this . List is a raw type which could be the list of Objects such as Strings,Wrappers and user defined objects .

public List<Statements> statements = new ArrayList<Statement.Statements>(); 

The above code says that statements is the reference to the ArrayList of Statement Objects . It does specifies that list would contain Statements objects which is inner class to the Statement class.

List<?> is used to say the list of unknown type.

Comments

1

The statements List can hold only Statements object. It is called Generics. Check this for sample programs.

http://www.java2s.com/Code/Java/Language-Basics/Asimplegenericclass.htm

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.