Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part III: Scope of Variables ( 7 Points ) The Java language defines eight basic types that are called the primitive types. These types are
Part III: Scope of Variables Points
The Java language defines eight basic types that are called the primitive types. These types are not defined
by classes and, therefore, variables of these types are not objects. These types are the necessary building
blocks for all programs and represent numbers, both integers and floating point types, single characters
and the boolean values true and false.
Date Members
Data members are declared inside the class and outside of any method.
Data members are constants if they are modified by final, and variables, otherwise.
If a data member is modified by static, it belongs to the class. There is only one version of a class data
member and it is shared by all objects of this type.
If a data member is not modified by static, it is an instance data member. Each object of this type
has its own version of an instance data member.
Data members are modified by access modifiers. So far we have used the public and private access
modifiers. Additional access modifiers are protected and to use no modifier. These modifiers determine
which outside classes have access to the data members. A complete explanation of this topic will be
given at the end of this course or the beginning of the next course.
All data members, class or instance data, may be accessed in any instance method defined in the class.
Class methods, those that are modified by static, may only access class data members. This makes
sense, since class methods can be invoked on the class rather than on an object.
Local Variables
Local variables are declared inside methods and include a methods formal parameters.
A formal parameter of a method is known throughout the body of the method.
A local variable declared inside the body of a method is known from the point at which it is declared
to the end of the block in which it is declared.
Local variables are never modified by the access modifiers since they may only be accessed in the
method. They also are never modified by static.
Program
A class defines a data type. The code below defines a data type called Scope This is used for illus
tration purposes only, the actual purpose of this class is immaterial. Read the code for understanding
and then answer the questions that follow.
pub l ic c l a s s Scope
pr ivate s t a t i c in t one ;
pr ivate in t two ;
pub l ic in t t h r e e ;
pub l ic s t a t i c f i n a l in t FOUR ;
pub l ic Scope in t w in t x
i s th e autoin c r em en t o p e r a t o r and adds one onto th e v a l u e
one;
two w ;
t h r e e x ;
pub l ic void change in t x
in t y one x ;
two one y ;
t h r e e t h r e e FOUR;
pub l ic S t r i n g t o S t r i n g
return one one two two t h r e e t h r e e ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started