• 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

var-args

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Dear Friends please Explain me how line1 is working.and how many objects are created by call of line1.
Thank You.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recently cleared SCJP. I may be having some idea:
varargs means Zero or more arguments

In line 8:
new Gabc() -> Called constructor with zero argument i.e. correct
new Gabc() -> Called constructor with zero argument i.e. correct
new Gabc(Gabc object 1, Gabc object 2( -> Called constructor with two arguments of type Gabc, which is allowed by constructor(zero or MORE argument)


--Deepesh Deomurari
SCJP - 94%
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gabc bc=new Gabc(new Gabc(),new Gabc()); This compiles fine. I think three objects are created. But tell me how new Gabc() worked as there is no default constructor in the class ?
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so where an argument is expected and var-args is used as the parameter, no argument at all can also be an option!
 
aruna dabas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepesh calling the constructor is fine to me what i am not getting is when constructor is called with two arguments,how many objects are created.javascript:emoticon('');
 
Deepesh Deomurari
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only ONE Object, so total will be three, two are referred by member Object of newly created class(ob).
remember one constructor run at each object creation.
It NEVER depends on number of parameters, parameters are used for MATCHING the constructor type.
 
aruna dabas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And each object created has refernce to an array of type Gabc.i am also not getting this "bc.ob[1].bc".please explain.
 
Deepesh Deomurari
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Similar solution
bc.ob[1] ->Object of Gabc
bc.obp[1].ob-> element of Gabc with name ob i.e. Array of Gabc
In next line you are creating array of two objects
1. new Gabc()
2. bc.ob[0] which is object of Gabc type
so bc.ob[].ob will be set to array of two variables.

One more things, sorry for last updates, at line1 parameter will not be LOST because it is still referred by ob[];

Vararg converts there argument to array so object created at line 1 will also have reference to two objects passed as parameter.
bc.ob[1] means SECOND object created at LINE 1 in parameter
bc.ob[0] means FIRST object created at LINE 1 in parameter
 
Evacuate the building! Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic