Accessing Instance Variables and Methods
Accessing Instance Variables and Methods
Instance variables and methods are accessed via created objects. To access an
instance variable, following is the fully qualified path −
/* First create an object */
ObjectReference = new Constructor();
Example
This example explains how to access instance variables and methods of a
class.
Live Demo
If we compile and run the above program, then it will produce the following result
−
Output
Name chosen is :tommy
Puppy's age is :2
Variable Value :2
Java Package
In simple words, it is a way of categorizing the classes and interfaces. When
developing applications in Java, hundreds of classes and interfaces will be
written, therefore categorizing these classes is a must as well as makes life
much easier.
Import Statements
In Java if a fully qualified name, which includes the package and the class name
is given, then the compiler can easily locate the source code or classes. Import
statement is a way of giving the proper location for the compiler to find that
particular class.
For example, the following line would ask the compiler to load all the classes
available in directory java_installation/java/io −