class - I'm having a hard time understanding Java objects and classes -
example 1
/** *program name: cis36l0411.java *discussion: class -- data members * method members */ class cis36l0411 { public static void main( string[] args ) { dataonly data1 = new dataonly(); system.out.println( "dataonly\tlimit\t\t" + data1.limit ); system.out.println( "\t\tintmem\t\t" + data1.imem ); system.out.println( "\t\tdoublemem\t" + data1.dmem ); methodonly method1 = new methodonly(); method1.printfunc( 5 ); method1.printfunc( "methodonly object!" ); method1.printfunc( data1.limit ); return; } } class dataonly { final int limit = 100; //constant , package mode or access int imem; //package mode or access double dmem; //package mode or access } class methodonly { void printfunc( int ia ) //package mode or access { system.out.println( "the int value " + ia ); return; } public void printfunc( string str ) //public mode or access { system.out.println( "the string printed " + str ); return; } }
i went this site , read it, still confused.
dataonly data1 = new dataonly();
know line creates object. can break line down me? each word do?dataonly
class? type?data1
variable? thinknew dataonly
reference location. ,()
variables in location? correct?how did print
data1.limit
,data1.imem
,data1.dmem
? did print looking @ location ofdataonly()
?dataonly()
reference classdataonly
?i'm lost on whole process of
methodonly
object.
1) dataonly data1 = new dataonly(); know line creates object. can break line down me? each word do? dataonly class?type? data1 variable? think new dataonly reference location. , () variables in location? correct?
the line means create variable named "data1" of type dataonly. create new object of type "dataonly" , make variable point it.
2) how did print data1.limit, data1.imem, data1.dmem? did print looking @ location of dataonly()? dataonly() reference class dataonly?
dataonly template object (a class). print using object in memory created template print values.
3) i'm lost on whole process of methodonly object.
objects can contain both data , perform functions depending on tempate (class) created from. methodonly class appears defined contain code , no data. dataonly class seems defined store values , not actions.
summary
think easiest way think of class blue-print object. objects created (using "new" keyword) based on these blueprints , stored in memory. work done objects, use classes tell type of object want.
Comments
Post a Comment