• 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

Return Type

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
…and why on earth are you putting code like lines 31‑32 into a constructor?
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try to explain in different way:

line 22: you buy a dog for your little sister
line 23: you say: I bought you a dog, his name is dog, whatever your name is "bark"
line 24: dog, from now on, your name is "Jacky", so next time I'll call you a name when I ask you to bark

What is wrong in this sequence?
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:…and why on earth are you putting code like lines 31‑32 into a constructor?



Because

Sachin Tripathi wrote:
Now in the constructor of Dogs2 ,do this:
Dogs2(Dog d)
{
System.out.println(d.getname());
}

This will return jacky,that you had assigned in Dog class through setname

 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:I'll try to explain in different way:

line 22: you buy a dog for your little sister
line 23: you say: I bought you a dog, his name is dog, whatever your name is "bark"
line 24: dog, from now on, your name is "Jacky", so next time I'll call you a name when I ask you to bark

What is wrong in this sequence?



Line 23 comes before 24???
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piyush Chaudhary wrote:Because


Is it your all explanation?

You got suggestion from Sachin Tripathi - probably not the best one, nevertheless, but why you did that?
Do you have your own explanation why you did that? Or you just copied/pasted without any understanding why was it for?
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Liutauras Vilda...
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Piyush Chaudhary wrote:Because


Is it your all explanation?

You got suggestion from Sachin Tripathi - probably not the best one, nevertheless, but why you did that?
Do you have your own explanation why you did that? Or you just copied/pasted without any understanding why was it for?



Yes, It is my all explanation, Because I am trying that code only & that's why I put it in reply for checking is this what he(Sachin) is saying!!!
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote:

To grasp it more firmly make setname() private
And try accessing it inside Dog
class
And keep getname() public .
Use getname() outside Dog class to see the name assigned by setname().

Try to find its advantage



How's this Possible to use getname() outside Dog class to see the name assigned by setname() Private??
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ShowSomeEffort
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did,, But its showing "null" in the name space..
 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello hello .I am back again
Its not me to blame ,when i posted i was tired and bored after attending boring lectures from 9 am to 5 pm(continous).well there is no escaping i had made a blunder and i will be correcting it
So sorry Piyush ,my friend.You should invoke setname method first before passing reference to the constructor
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this is not the only way to do this .You shouldn't be doing this in constructor.As it is not meant for it
First execute the code as it is(by making changes,what i suggested in my last post)
Then i will be helping you to achieve same thing by better ways.
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Really, Its getting mindfreaking. I can't understand that how to getname outer than class while having setname private?
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have missed the point.
You can have

System.out.println(d.getname());

Inside constructor of Dogs
Then you will be getting the name outside the class.
In your previous code you made setname public make it private and enjoy the fun.
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sachin.. But Actually I can't understand what we are doing in statements

and

Will you please?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first statement you are passing a Dog reference to the Dogs constructor and assigning that Dogs object to the reference e.
The second piece of code is incomplete and is presumably the constructor call on its own.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first statement we are creating object of "Dogs" and sending reference of Dog object( whose name is Jacky) to "Dogs" constructor

In the second statement you are receiving the reference of "Dog" object( whose name is Jacky) and invoking a method


Just remember one thing object is a buffer in heap,we always need a reference to operate it.
Reference variable act like TV remote for the object they are assigned for
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you face any problem with object reference ,do make a new topic ,as aim of this thread has already been achieved.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Tripathi wrote: . . .

In the second statement you are receiving the reference of "Dog" object( whose name is Jacky) and invoking a method


Just remember one thing object is a buffer in heap,we always need a reference to operate it.
. . .

No, you aren't invoking a method. That bit isn't right.
What do you mean by “buffer”?
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. I was talking in reference to his code actually
In that statement "he is just receiving the reference" after that statement he is using that reference to invoke a method
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Talking about buffer. Buffer actually means "things that forms barrier from antagonistic things"

In the similar way object hold all its field and forms a barrier,and protect them from being getting affected.


Should I continue using buffer ,or it is ambiguous?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the word buffer in a computing sense means an area of memory where information is collected until it is needed.
He is not invoking any methods in the code posted in that post. The first part as we agreed is a simple assignment to a newly‑created object. The second piece, in the context, looks like a simple constructor call. And it can only be part of the code; you can't write that without at least a new operator.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I know it is just a constructor call ,which is a part of program, but as I am helping him since he posted so I know he would be using that reference to invoke method get name()(I myself suggested him,so I know it well. )
You don't need to worry

Thanks for the buffer thing
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic