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>();
Community
  • 1
  • 1
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
  • http://docs.oracle.com/javase/tutorial/java/generics/index.html – Brian Roach Dec 29 '11 at 07:11
  • I added the "generics" tag for you. I know you didn't know about it, but I did. Hopefully it will lead to some trivially similar related questions. –  Dec 29 '11 at 07:20
  • http://stackoverflow.com/questions/1844770/what-does-list-mean-in-java-generics , http://stackoverflow.com/questions/2218216/in-java-what-does-it-mean-when-a-type-is-followed-by-angle-brackets-as-in-list –  Dec 29 '11 at 07:22

4 Answers4

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).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
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

nmjohn
  • 1,432
  • 7
  • 16
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.

Community
  • 1
  • 1
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
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

Vaandu
  • 4,857
  • 12
  • 49
  • 75