

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of passing references in java, using the examples of demo3.java and passingreferences.java. It explains that all types are passed by value, but if a reference type's value is changed, the change is reflected back in the caller. The document also introduces instance methods and the point class, which requires methods for constructors, accessors, mutators, and a tostring() method.
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Last time we looked at some class methods, and what happens when the methods are called. Our last example looked what happens when the FP are a reference type. Let's look at Demo3.java And at PassingReferences.java All types are passed by value - it does not matter if the type of the formal parameter is a primitive type or a reference type. This means that if we change the value of a formal parameter, that change is not reflected back in the calling method. However , Let’s look at another example: PassingReferences2.java So, we can see that method f is working with a distinct JFrame, but method g is working with the reference that was passed to it. Repeat KEY POINT: If we change the value that is referenced by a reference type, that change is reflected back in the caller. This is a subtle but important distinction! Instance Methods Remember, instance methods require an object - we invoke instance methods using an object's name followed by '.' and the method name. String s = "Hello"; int l = s.length(); JFrame myFrame = new JFrame( "The Title" ); myFrame.setSize( 200, 300 ); Instance methods are defined without the keyword 'static': [public]