Methods and References Flashcards
What are reference diagrams?
Reference diagrams are diagrams that show how data is stored by objects and how computation is performed on those objects. They can help developers understand how data structures are constructed and how objects interact with each other.
Why are reference diagrams useful?
Reference diagrams are useful because they help developers visualize the structure and behavior of complex systems. By understanding how objects are constructed and how they interact with each other, developers can write more efficient and effective code.
What types of data are represented in reference diagrams?
Reference diagrams can represent both class types and simple types. Class types are objects that are instances of a class, while simple types are basic data types like integers, booleans, and strings.
How do class types and simple types pass parameters?
Class types pass parameters by copying references to objects, while simple types pass parameters by copying values. When a class type is passed as a parameter, the reference to the object is copied, so any changes made to the object in the method will be reflected in the original object. When a simple type is passed as a parameter, a copy of the value is made, so any changes made in the method will not affect the original value.
What is the benefit of understanding reference diagrams before programming?
Understanding reference diagrams before programming can help developers create more effective and efficient code. By visualizing the structure and behavior of a system, developers can identify potential issues and design solutions that take advantage of the strengths of the system. This can save time and effort in the programming process, and result in better code overall.
How do you call a static method?
To call a static method, you use the name of the class followed by the method name, like this: ClassName.methodName(). For example, if you have a static method called incCheckins() in the Book class, you could call it like this: Book.incCheckins().
How do you call a non-static method?
To call a non-static method, you use the name of the object followed by the method name, like this: objectName.methodName(). For example, if you have an object called b1 of the Book class, and you want to call the setAuthor() method on that object, you could call it like this: b1.setAuthor(“Me”). Within the method, the object that the method was called on is referred to using the “this” keyword.
What is a static instance variable?
A static instance variable is a variable that is associated with a class, rather than with a particular instance of the class. This means that there is only one copy of the variable, which is shared among all instances of the class. Static instance variables are declared using the “static” keyword, and they can be accessed from within static methods.
What is a non-static instance variable?
A non-static instance variable is a variable that is associated with a particular instance of a class, rather than with the class itself. This means that each instance of the class has its own copy of the variable. Non-static instance variables are declared without the “static” keyword, and they can be accessed from within both static and non-static methods.
What is the difference between a static instance variable and a non-static instance variable?
The main difference between a static instance variable and a non-static instance variable is that a static instance variable is associated with the class, while a non-static instance variable is associated with a particular instance of the class. This means that there is only one copy of a static instance variable, which is shared among all instances of the class, while each instance of the class has its own copy of a non-static instance variable. Additionally, static instance variables can only be accessed from within static methods, while non-static instance variables can be accessed from both static and non-static methods.
Can a static method access a non-static instance variable?
No, a static method cannot access a non-static instance variable, because the variable is associated with a particular instance of the class, rather than with the class itself. To access a non-static instance variable from within a static method, you need to pass a reference to an instance of the class as a parameter.
Can a non-static method access a static instance variable?
Yes, a non-static method can access a static instance variable, because the variable is associated with the class, rather than with a particular instance of the class. However, you should be careful when accessing static instance variables from within non-static methods, because the value of the variable is shared among all instances of the class.
What happens when a value is passed as a parameter to a function?
A value is copied into a local parameter for the function.
What happens to the value passed as a parameter once the function terminates?
Once the function terminates, the value is destroyed.
For simple types, what is copied into the local parameter of a function?
For simple types, the actual value stored in the variable is copied into the local parameter of a function.