2

I am very new to java, infact to the programming world, and am confused about classes and objects. Why do we need a class to create an object? How and when these objects find physical reality? And why do we need to create a software model of a physical thing? Where are these objects and classes stored? Please help. Gone through 10+ websites and 4 books ,couldn't get the clear picture.

  • Jon Skeet (?) has a good reply on this somewhere .. as an analogy, a class is a "blueprint for a house" while and object *is* a "house" (and there can be *many* different houses [or objects] constructed from the same blueprint [or class] that differ in details such as color or address). – user2864740 Jan 09 '14 at 06:57
  • See http://stackoverflow.com/questions/3686647/whats-the-best-way-to-define-the-words-class-and-object-to-someone-who-hasn , http://stackoverflow.com/questions/3323330/difference-between-object-and-instance , http://stackoverflow.com/questions/2290422/what-is-the-difference-between-object-and-instance , http://stackoverflow.com/questions/13775226/what-is-the-difference-between-classes-and-object-instances etc. – user2864740 Jan 09 '14 at 07:01
  • (And no objects don't have "physical reality" excepting that they are "bits in memory somewhere" - however, they are "physical" in that each instance of a class [aka object] exists as a *separate* piece information.) – user2864740 Jan 09 '14 at 07:05

1 Answers1

0

A class declaration names the class and encloses the class body between braces. The class name can be preceded by modifiers. The class body contains fields, methods, and constructors for the class. A class uses fields to contain state information and uses methods to implement behavior. Constructors that initialize a new instance of a class use the name of the class and look like methods without a return type.

You control access to classes and members in the same way: by using an access modifier such as public in their declaration.

You specify a class variable or a class method by using the static keyword in the member's declaration. A member that is not declared as static is implicitly an instance member. Class variables are shared by all instances of a class and can be accessed through the class name as well as an instance reference. Instances of a class get their own copy of each instance variable, which must be accessed through an instance reference.

You create an object from a class by using the new operator and a constructor. The new operator returns a reference to the object that was created. You can assign the reference to a variable or use it directly.

Instance variables and methods that are accessible to code outside of the class that they are declared in can be referred to by using a qualified name. The qualified name of an instance variable looks like this:

            objectReference.variableName

The qualified name of a method looks like this:

           objectReference.methodName(argumentList)

or:

           objectReference.methodName()

The garbage collector automatically cleans up unused objects. An object is unused if the program holds no more references to it. You can explicitly drop a reference by setting the variable holding the reference to null.

for further understanding go through these links:: http://docs.oracle.com/javase/tutorial/java/javaOO/summaryclasses.html http://www.tutorialspoint.com/java/java_object_classes.htm http://www.programmerinterview.com/index.php/java-questions/difference-between-object-and-class/

Charles Stevens
  • 1,568
  • 15
  • 30