• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why & when a object should be created from class

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic