• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why & when a object should be created from class

 
jacob deiter
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why & when a object should be created from class. Why a class can�t has static fields and method only.

everybody says in OOP ,a object should be created from a class����

In oops ,generally break the functionality in many class, as per oop standard, if I want to finish a task ,then all class should interact, here without creating a object I can finish all the task

I explain with below example then it will be more clear

Interest calculator

Consider a table has principle amount ,period and interest rate.to calculate the functionality simply I write a method in a class to get all data from table and calculate the interest, here why I need a object creation

String is a class. I want to hold different value of string, and then I have to create more objects otherwise it not needed.
String a=�w�;
String h=�H�;

In ORM like hibernate, class represent the table structure so for each record o object should be created. this the scenario where object creation exactly needed.

These thing confuse me for long time. Please correct if am wrong
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jacob Deiter,

Remember that in Java a class is just a bunch of method and/or variables. But the actually definition of a class is: "collection of things sharing a common attribute". This definitions refers to the use of objects. However, objects are also needed as you mentioned.

You can have a class composed only of methods that can help the program to make calculations. A good example would be java.lang.Math, you don't need an object there.

Objects are primarily just another form for storing things and/or returning value to make calculations. You would have to use an object whenever the class requires lots of public/private variables.

E.g.:



This would obviously need objects since you're likely to implement more than 1 person in the program.

Another example would be when the class NEEDS private variables to make calculations and return values to the user:



readln() doesn't require any parameters because when you create an object you use the constructor to initialize filename. So readln() can use that variable to do whatever it must.

Understood? more or less?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic