1

I was studying livewallpaper in this site. However something there is something that i dont understand.

Example in the code of the tutorial there a class named MyPoint

public class MyPoint {
  String text;
  private int x;
  private int y;

  public MyPoint(String text, int x, int y) {
    this.text = text;
    this.x = x;
    this.y = y;
  }
} 

then after he created a MyWallpaperService class. Inside of that class there is a line of code like this

 private List<MyPoint> circles;
    private Paint paint = new Paint();
    private int width;
    int height;
    private boolean visible = true;
    private int maxNumber;
    private boolean touchEnabled;

public MyWallpaperEngine() {
  SharedPreferences prefs = PreferenceManager
      .getDefaultSharedPreferences(MyWallpaperService.this);
  maxNumber = Integer
      .valueOf(prefs.getString("numberOfCircles", "4"));
  touchEnabled = prefs.getBoolean("touch", false);
  circles = new ArrayList<MyPoint>();
  paint.setAntiAlias(true);
  paint.setColor(Color.WHITE);
  paint.setStyle(Paint.Style.STROKE);
  paint.setStrokeJoin(Paint.Join.ROUND);
  paint.setStrokeWidth(10f);
  handler.post(drawRunner);
}

You can see the part of code has

private List<MyPoint> circles;

This is the part that i dont understand? What is happening in here? What will List<MyPoint> pass in the circles? Anyone knows what to call this? is this list reffering to a class? Cause im not sure on my title. Thank you..

CodeAndWave
  • 1,586
  • 3
  • 23
  • 33
  • 3
    The angle brackets <> are [generics](http://docs.oracle.com/javase/tutorial/java/generics/why.html) in Java. It is creating a list that can only hold `MyPoint` objects – n00begon Aug 29 '12 at 02:50
  • related: http://stackoverflow.com/questions/1286005/what-is-typetype-called – Paul Bellora Aug 29 '12 at 02:53

3 Answers3

3
private List<MyPoint> circles;

States that circles is a List of type MyPoint, (ie. it will hold objects of type MyPoint).

circles = new ArrayList<MyPoint>();

Now in the above line you are assigning the ArrayList object of type MyPoint to the Object Reference Variable of type List.

This is called as Interface Polymorphism.

List is an Interface, where as ArrayList a Concrete Class which implements List.

Eg:

public class Dog{

   private String dName;
   priavet int dAge;

   public Dog(String dName, String dAge){

      this.dName = dName;
      this.dAge = dAge;


   }



   public String getDName(){

        return this.dName;

   }

   public String getDName(){

        return this.dAge;

   }

}


public class Test{


 public static void main(String[] args){

     List<Dog> dAList = new ArrayList<Dog>();

     dAList.add(new Dog("Tommy",5));
     dAList.add(new Dog("Stark",2));

     for(Dog d : dAList){   // Iterating over the List of Dog objects

           System.out.println(d.getDName());
           System.out.println(d.getDAge());

         }


     }



}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

Its creating a list of objects. A List of MyPoint type objects MyPoints in your case refers to the (x,y) coordinates of the circle(as you referred)

G_S
  • 7,068
  • 2
  • 21
  • 51
1

The List<MyPoint> object is, as the name suggests, a list of MyPoint instances which, judging from the code, represent the centers of your circles. The private identifier simply indicates that it cannot be accessed from outside of the class in which it is defined.

arshajii
  • 127,459
  • 24
  • 238
  • 287