• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why is this legal?

 
Ranch Hand
Posts: 66
Android IntelliJ IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is it possible to create an object of type Class within the class definition? This seems very chicken vs. egg to me....


public class Why
{
public static void main(String[] args)
{
Why myWhy= new Why();
}
}
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than asking why you can, why shouldn't it be?
 
Keith Rainey
Ranch Hand
Posts: 66
Android IntelliJ IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Classes are typically presented as 'blueprints' for objects. Following the analogy, it doesn't make sense to me to be able to create the object from an incomplete blueprint. It seems like a circular reference to me.
This obviously is a conceptual failure on my part as the code compiles and runs.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this discussion it is important to remember the difference between a Class and an Object.
The class is static and always exists.

To get an object it must be created with the new statement.

The Class and Object are 2 different entities.
Think of the Class as a blueprint or plans to create something and the object that something that is created.

The Class is neither the chicken nor the the egg. The class is the creator, or what is used to create.

I hope this helps.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is legal in other object orient languages as well -- in fact, I can't think of one OO language where this isn't allowed.

Henry
 
Bear Bibeault
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Keith Rainey wrote:it doesn't make sense to me to be able to create the object from an incomplete blueprint.


Why is it incomplete? The code, including the new operator, isn't executed at compile time, but at run time. The class definition exists long before the code within it ever gets a chance to run.
 
and POOF! You're gone! But look, this tiny ad is still here:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic