Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

y aint here class <classname>=new <classname> used and instead only class <classname> used

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why aint here Runtime rt=newRuntime used
and i have heard that refernce variables in method cannot have default value
so
what is being assigned at Runtime rt=Runtime.getRuntime()
and how come predefined methods like
rt.totalMemory()
rt.freeMemory()
used if rt reference value isnt assigned anything that is if its not declared as
Runtime rt=new Runtime();

 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:why aint here Runtime rt=newRuntime used
and i have heard that refernce variables in method cannot have default value
so
what is being assigned at Runtime rt=Runtime.getRuntime()


That is how the Runtime class is designed. There aren't any public contructors that can be used to construct a Runtime instance. Instead, it uses a factory method, which returns the singleton instance that has been contructed by the class.

Vishal Hegde wrote:and how come predefined methods like
rt.totalMemory()
rt.freeMemory()
used if rt reference value isnt assigned anything that is if its not declared as
Runtime rt=new Runtime();


Just because you didn't directly use the new operator, doesn't mean that the instance hasn't been created -- it was. It was instantiated and returned when you called the getRuntime() method.

Henry
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:what is being assigned at Runtime rt=Runtime.getRuntime()
used if rt reference value isnt assigned anything that is if its not declared as
Runtime rt=new Runtime();

getRuntime() is a static method which returns the Runtime object associated with the current Java application, so the "rt" reference refers to an instance of the Runtime class.
and how come predefined methods like
rt.totalMemory()
rt.freeMemory()

You still can invoke any instance method(s) using "rt" reference.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Instead, it uses a factory method, which returns the singleton instance that has been contructed by the class


what is a factory method and whats singleton instance
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:
Henry Wong wrote:
Instead, it uses a factory method, which returns the singleton instance that has been contructed by the class


what is a factory method and whats singleton instance


What did you find out when you searched for those terms? You did search for those terms right?
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Real words, not "y".
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic