Please explain “this” to me

前端 未结 6 1368
轮回少年
轮回少年 2021-01-01 05:03

I\'ve read hundreds of explanations on \"this\" in java and I\'m really having trouble grasping it. I\'m learning android and java side-by-side, I know it\'s harder that wa

6条回答
  •  青春惊慌失措
    2021-01-01 05:27

    Think of this as "itself". If you pass this to a method, you're simply passing an instance of the object to the method.

    ie: Student is an object, as is Classroom. If I want to add a Student to the Classroom, I might tell Student to add itself to the classroom (classrooms can't find students, can they?). So, I will say student.addToClassroom(new Classroom(), this);

提交回复
热议问题