Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a Java Question if you need more information/clarification let me know I am confused about when the value of an instance variable is

This is a Java Question if you need more information/clarification let me know
image text in transcribed
image text in transcribed
I am confused about when the value of an instance variable is established. I thought it was established by assigning the value of the parameter to the instance variable. But the lesson that I am working on says that once an argument is passed during a method call, it will be used to give an instance variable an initial value. If that is the case, what is the difference between parameter assignment to an instance variable vs assigning value to an instance variable? Overview of What the program says To create objects with dynamic, individual states, we'll use a combination of the constructor method and instance fields. In order to assign a value to an instance variable, we need to alter our constructor method to include parameters so that it can access the data we want to assign to an instance. We've already seen a parameter in the main0 method: String[] args, but this is the first time we're using the parameter value within a method body. The Car constructor now has a parameter: String carColor: When a String value gets passed into Car0, it is assigned to the parameter carColor. Then, inside the constructor, carColor will be assigned as the value to the instance variable color. public class Car String color / / constructor method with a parameter public car string carcolor I/ parameter value assigned to the field color carcolor public static void main string args // program tasks Then the lesson states Now that our constructor has a parameter, we must pass values into the method call. These values are referred to as arguments; once they are passed in, they will be used to give the instance fields initial value. Here we create an instance, ferrari, in the main()method with "red" as its color field: public class Car string color public car String carcolor I/ assign parameter value to instance field color carcolor public static void main string args I/ parameter value supplied when calling constructor Car ferrari new Car "red" I am confused because I thought it was already established that the value of the instance variable was the value from the parameter that was assigned to it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions