Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Getting to Instance Variables and Methods


Download 0.82 Mb.
Pdf ko'rish
bet10/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   6   7   8   9   10   11   12   13   ...   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

Getting to Instance Variables and Methods:
Variables  and  methods  are  gotten  to  by  means  of  made  objects  of  classes.  To  get  to  a
variable, the qualified way ought to be the following:
The following statement creates an object.
Newobject = new Constructormethod();
The following statements can be used to access the variable and method associated with
the object.
Newobject.variablesname;
Newobject.methodsname();
A sample implementation of this concept is given below:
public class Dog{
int dogAge;
public dogAge(String dogname){
System.out.println(“Dog Name Passed is :” + dogname );
}
public void initAge( int dogage ){
dogAge = dogage;
}
public int getDogAge( ){
System.out.println(“Dog’s present age is:” + dogAge );
return dogAge;
}


public static void main(String []args){
Dog myDog = new Dog( “jimmy” );
myDog.initAge( 5 );
myDog.getDogAge( );
System.out.println(“Variable dogAge Value is:” + myDog.dogAge );
}
}
Upon  compilation  and  execution  of  the  following  code,  you  shall  be  able  to  see  the
following result.
Variable dogAge Value is: 5

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   6   7   8   9   10   11   12   13   ...   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling