Question:
What is difference between object of class and instance of class?
vicky
2010-11-25 22:53:49 UTC
Please help me i am beginner in java ..What is difference between object of class and instance of class???..
please give the answer in simply words because i am a beginner of java ....
Four answers:
SUDHAKAR Kuruvada
2010-11-26 01:14:25 UTC
A class is the structure of an object, meaning the definition of all items that an object is made up of. An object is therefore the "result" of a class. In reality, an object is an instance of a class, which is why can use interchange the terms object or instance (or even occurrence).



A class is made up of two parts:



•Attributes (often called member data): this is the data that refers to the object's state

•Methods (often referred to as member functions): these are functions that can be applied to objects

If we have a class called car, the objects Peugeot 406 and Renault 18 will be instances of this class. There may also be other objects Peugeot 406, differentiated by their model number. Furthermore: two instances of a class may have the same attributes but may be considered separate distinct objects. In a real world scenario: two T-shirts may be identical, but they are however distinct from each other. However, if we mix them together it is impossible to distinguish one from the other.
2016-02-28 00:57:17 UTC
Class is a definition, object is an instance at runtime. For example, I might make an Animal class and then have an instance to represent Fido the neighbor's dog. Methods are operations that are defined on classes. Some methods (static) are to be executed against a class itself. Others (non-static) can only be executed against an instance of the class. Constructors are special methods that are called when an instance of a class is created. In Java at least, a Constructor is identified by having a method name that is exactly the same as the class name: public class Animal { String name; public Animal( String aName ) { // Constructor name = aName; } } Using the above definition, I could write this elsewhere and it would work by calling the given constructor: Animal neighborsDog = new Animal( "Fido" ); private/public indicate levels of access. If you make a constructor private, for example, then only code within the class can "see" it to create a new instance via that constructor. If you make a method private, then only code within the same class can invoke it. In general, the methods that you expect the outside world to call should be public, while the methods that you use just for convenience and internal use only should be private. (Actually there are two other levels in Java in between, protected and package level. Using the "protected" keyword exposes the field/method to other classes in the same package AND to subclasses. Using no keyword exposes the field/method to other classes in the same package only.) void just means "does not return a value." The definition of a method includes optional access control (public/private), then a return type (or void if none), then the name of a method: public int getSum() { return 3 + 4; } private void sayHello() { System.out.println("Hello."); // It would be illegal to return a value. }
?
2010-11-26 00:11:55 UTC
A class is basically a definition, and contains the object's code. An object is an instance of a class. For instance, there is one java.lang.String class, but you can instantiate any number of distinct java.lang.String objects (instances). While a class defines the instance variables than an object has, the instantiated object itself actually contains those variables. So to put it simply: An object is an instance of a class.



Class

Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features). One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). Classes provide modularity and structure in an object-oriented computer program. A class should typically be recognizable to a non-programmer familiar with the problem domain, meaning that the characteristics of the class should make sense in context. Also, the code for a class should be relatively self-contained (generally using encapsulation). Collectively, the properties and methods defined by a class are called members.



Object

A pattern (exemplar) of a class. The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Lassie is one particular dog, with particular versions of the characteristics. A Dog has fur; Lassie has brown-and-white fur.



Object-oriented programming provides several other concepts and features to make creating and using objects easier and more flexible, and the most important of these features is that of classes.



A class is a template for multiple objects with similar features. Classes embody all the features of a particular set of objects. When you write a program in an object-oriented language, you don't define actual objects. You define classes of objects.



For example, you might have a Tree class that describes the features of all trees (has leaves and roots, grows, creates chlorophyll). The Tree class serves as an abstract model for the concept of a tree-to reach out and grab, or interact with, or cut down a tree you have to have a concrete instance of that tree. Of course, once you have a tree class, you can create lots of different instances of that tree, and each different tree instance can have different features (short, tall, bushy, drops leaves in Autumn), while still behaving like and being immediately recognizable as a tree.



An instance of a class is another word for an actual object. If class is the general representation of an object, an instance is its concrete representation.



So what, precisely, is the difference between an instance and an object? Nothing, really. Object is the more general term, but both instances and objects are the concrete representation of a class. In fact, the terms instance and object are often used interchangeably in OOP language. An instance of a tree and a tree object are both the same thing.



In an example closer to the sort of things you might want to do in Java programming, you might create a class for the user interface element called a button. The Button class defines the features of a button (its label, its size, its appearance) and how it behaves (does it need a single click or a double click to activate it, does it change color when it's clicked, what does it do when it's activated?). Once you define the Button class, you can then easily create instances of that button-that is, button objects-that all take on the basic features of the button as defined by the class, but may have different appearances and behavior based on what you want that particular button to do. By creating a Button class, you don't have to keep rewriting the code for each individual button you want to use in your program, and you can reuse the Button class to create different kinds of buttons as you need them in this program and in other programs.
Toby
2010-11-25 23:23:23 UTC
these should explain it





* Instance methods can access instance variables and instance methods directly.

* Instance methods can access class variables and class methods directly.

* Class methods can access class variables and class methods directly.

* Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.





Simply, a class method ( aka "static method")is a property for the WHOLE class , so its's called as className.methodName().



BUT for instance methods, then each object have their own copy of them, so they r called as objectName.instanceMethodName().



Second difference :

-class ("static")methods can access ONLY static fields

-but instance methods can access ANY fields of the class .



A third a bit advanced difference is that class (Static) methods are bound to the class at compile time , while instance methods are bound to their objects at run time


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...