At the bottom of this post is a link with the answers to the free response question assignment for unit 4.
Static methods and variables and the this keyword. We discussed static variables and methods briefly in class before distance learning began. To review, the key points that you should know. A static variable is a class level variable (field) whose value is the same for EVERY instance of a class. If one instance of a class updates that variable to a new value, that new value is now referenced by every other instance of that class. Think of the fish program where every fish had its own individual ID number (non-static) variable. But when the fish spawned, they all had to be looking at the same available nextID number so that two different fish would not be assigned the same ID number or numbers would not be skipped. In the constructor class for the fish objects, when the fish spawned that fish would be given the current nextID number, and then the constructor had an instruction to increment the next ID number by one. So for example, if there were 20 fish, the nextID number would be 21. If fish number 3 spawned, it would call the constructor who would set the ID number of its child to 21 and change nextID to 22. Let's say fish 8 was the next one to spawn. It would then see the nextID of 22, assign that number to its child and increment the nextId to 23. This is a static variable - the value seen is common to all instances of a class and if one class object changes it, the change is now seen by all members of the class simultaneously. A class can have both static and non-static methods. Static methods in a class can only see and modify static variables in the class. It can not access the instance variables which are non -static (meaning specific to one individual instance) of a class object. Also, a static method can be called on the class itself. It is generally not called on an instance object of the class. For example: If you have a class called Stuff with a static method called doThis( ), the following syntax is allowable: Stuff.doThis( ); Call the static method on the class itself. You do NOT have to create an instance of Stuff. (More or less, you do not use a static method on an instance object of a class.) (As an FYI there are some work-around solutions, but generally you should not be calling a static method on an instance object of a class. ) The this keyword is used when the code is referring to the object that is currently calling a method. It is used in code to represent the object itself that made the call. The object is referring to itself. (similar to reflexive verbs in foreign language). It is often used in constructors so that the global variables of fields in a class are not confused with the instance variables of the object calling the constructor if they have the same exact name. Code examples using the this keyword: www.guru99.com/java-this-keyword.html Example where this is used to distinguish between same named object variable x and specific class instance variable called x. www.w3schools.com/java/ref_keyword_this.asp More examples of how this can be used in code: www.javatpoint.com/this-keyword With the last two post and the discussion of the topics this, static variables/methods and for each loops, we have pretty much covered the last of the curriculum that may be included on this year's AP exam. Check over your answers for free response question assignment number 4. Number 5 will be posted shortly. At this point, you should be starting to notice a similarity to the questions being asked. Email me if you are not sure why the answers are as shown. Answers to FR 4 drive.google.com/file/d/1nl6SVtRX2o19mnCrN-mJk3LRql8n3E2s/view?usp=sharing Comments are closed.
|