Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm having a hard time understanding Java objects and classes

Tags:

java

object

class

Example 1

/**
 *Program Name: Cis36L0411.java  
 *Discussion:   Class -- Data Members ONLY
 *                       Method Members ONLY
 */ 
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 and 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 is " + iA );

    return;
  }

  public void printFunc( String str ) //public mode or access
  {
   System.out.println( "The String is printed from  " + str );

    return;
  }
}

I went to this site and I read it, but I am still confused.

  1. DataOnly data1 = new DataOnly(); I know this line creates an object. But can someone break this line down for me? What does each word do? DataOnly is the class? type? data1 is the variable? I think new DataOnly is a reference to a location. And the () is the variables in the location? Am I correct?

  2. How did they print data1.LIMIT, data1.iMem, Data1.dMem? Did they print it by looking at the location of DataOnly()? Does DataOnly() reference class DataOnly?

  3. I'm just completely lost on the whole process of the MethodOnly object.

like image 672
CuriousStudent Avatar asked Oct 08 '10 01:10

CuriousStudent


1 Answers

1) DataOnly data1 = new DataOnly(); I know this line creates an object. But can someone break this line down for me? What does each word do? DataOnly is the class?type? Data1 is the variable? I think new DataOnly is a reference to a location. And the () is the variables in the location? Am I correct?

The line means create a variable named "data1" of the type DataOnly. Then create a new object of type "DataOnly" and make the variable point to it.

2) How did they print data1.LIMIT, data1.iMem, Data1.dMem? Did they print it by looking at the location of DataOnly()? Does DataOnly() reference class DataOnly?

DataOnly is just the template for an object (a class). The print is using an object in memory created from that template to print the values.

3) I'm just completely lost on the whole process of the MethodOnly object.

Objects can contain both data and perform functions depending on the tempate (class) it was created from. The MethodOnly class appears to be defined to only contain code and no data. The DataOnly class seems to be defined to just store values and not do any actions.

Summary
I think the easiest way to think of it is that the class is the blue-print for an object. Objects are created (using the "new" keyword) based on these blueprints and stored in memory. So all your work will be done with objects, you just use the classes to tell it what type of object you want.

like image 172
JohnFx Avatar answered Oct 01 '22 17:10

JohnFx