What is the Java way to use C++'s version of "this"

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Gatesunder
Posts: 120
Joined: Sat Feb 09, 2008 11:24 pm UTC
Location: KSU

What is the Java way to use C++'s version of "this"

Postby Gatesunder » Thu Apr 10, 2008 2:12 am UTC

I tried searching for something on this, but this is kind of an ambiguous word. Maybe you are wondering what this is. Well, this is this. In C++ there is a keyword called 'this' . . . Java has that keyword also, but from what I've seen it's used slightly differantly than C++. What I would like to know is how I get Java code that does that same thing as the following C++ code without creating a new object and by new object I mean a new object of type x, not a new object of type SomeOtherClass.

Code: Select all

// C++ Code
class x
{
public:
    SomeOtherClas * someFunction()
    {
         return new SomeOtherClass( this );
    }
private:
    int a;
    int b;
}


or even this

Code: Select all

// C++ Code
class x
{
public:
    SomeOtherClas & someFunction()
    {
         return *this;
    }
private:
    int a;
    int b;
}


basically I want to pass a pointer ( I know, in Java everything is a pointer, though they call them referances ).

The difficult thing about asking this question is the inherent ambiguity in the terminology assigned to these specific parts of C++ and Java. Hopefully you understand what I'm trying to ask here and I think, if you only have experience with coding in Java you might not, but feel free to offer an explanation based on what you think I am asking.
I'm wrong 99% of the time, but some day that 1% will be better than the rest ...

coppro
Posts: 117
Joined: Mon Feb 04, 2008 6:04 am UTC

Re: What is the Java way to use C++'s version of "this"

Postby coppro » Thu Apr 10, 2008 2:20 am UTC

I think I understand what you want to do... in Java, everything is a reference, right? So if you want to pass by reference (so that a new object is not created), just pass "this". On the other hand, if you want to pass by value (to create a new instance, so that changes won't affect the calling object), create a new object by passing "new this(this)". Similarly for returning by reference or value.

Is that what you wanted?
Last edited by coppro on Thu Apr 10, 2008 2:31 am UTC, edited 3 times in total.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: What is the Java way to use C++'s version of "this"

Postby Rysto » Thu Apr 10, 2008 2:21 am UTC

I honestly have no idea what you're asking here. I can't think of any way that this operates differently in Java from C++*. Are you asking about implicit conversions? That has nothing to do with the this keyword.



* Except that this can be called as a function in the first line of a constructor to call another constructor of the same class in Java, but that has nothing to do with your question.

User avatar
ash.gti
Posts: 404
Joined: Thu Feb 07, 2008 1:18 am UTC
Location: Probably a coffee shop.

Re: What is the Java way to use C++'s version of "this"

Postby ash.gti » Thu Apr 10, 2008 2:24 am UTC

'this' in C++ is a pointer to the location of the object you made.


SO...

Code: Select all

#include <iostream>
#include <string>

using namespace std;

//--

class CSomeClass
{
public:
  void Print(const string text) const
  { 
    cout<<text<<endl;
    cout<<"The address of that object is: " << this << endl<<endl;
  }
};

//--

int main()
{
  CSomeClass obj_1;
  CSomeClass obj_2;
 
  obj_1.Print ("Object 1 made me print this");
  obj_2.Print ("Object 2 made me print this");

  return 0;
}


In C++ will give you the address of the object printed out.

**EDIT**
In java, 'this' is a scope operator. Its for resolving the operators if there are 2 with the same name.

I don't know what I was thinking. Sorry wrong language ><

In Java this() calls the constructor of a class with different parameters.

so, Java:

Code: Select all

public class Test {
       String name;

       Test(String input) {
              name = input;
       }
       Test() {
              this("Hello");
       }
  ...
}


Which results in the constructor without parameters defaulting to "Hello".
Last edited by ash.gti on Thu Apr 10, 2008 2:36 am UTC, edited 1 time in total.
# drinks WAY to much espresso

User avatar
Gatesunder
Posts: 120
Joined: Sat Feb 09, 2008 11:24 pm UTC
Location: KSU

Re: What is the Java way to use C++'s version of "this"

Postby Gatesunder » Thu Apr 10, 2008 2:35 am UTC

coppro wrote:I think I understand what you want to do... in Java, everything is a reference, right? So if you want to pass by reference (so that a new object is not created), just pass "this". On the other hand, if you want to pass by value (to create a new instance, so that changes won't affect the calling object), create a new object by passing "new MyClass(this)". Similarly for returning by reference or value.

Is that what you wanted?


Yeah, that's basically what I was wondering, but I tried using "this" in Java and it kept saying something about it having to be the first argument in the constructor or some junk. So basically it won't let me use "this" in Java like I want to use it in C++, but What I would like to know is how can I get the same functionality in Java that the "this" keyword in C++ gives me in C++.

For the Java guys maybe this explanation helps a bit better
-------------------------------------------------------------------------
I just want to pass or return a reference to the object calling the function I want to do this (<---the grammatical this) in.
I'm wrong 99% of the time, but some day that 1% will be better than the rest ...

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: What is the Java way to use C++'s version of "this"

Postby Rysto » Thu Apr 10, 2008 2:46 am UTC

You *can* use the this keyword like you can in C++. Show us the Java code that's giving you your problems; that should clarify things immensely.

User avatar
ash.gti
Posts: 404
Joined: Thu Feb 07, 2008 1:18 am UTC
Location: Probably a coffee shop.

Re: What is the Java way to use C++'s version of "this"

Postby ash.gti » Thu Apr 10, 2008 2:55 am UTC

Code: Select all

public class Someclass {
public Someclass getReferenceToSelf() {
return this
}
}


Try that...

Eh... I am confusing myself. Sorry

http://xahlee.org/java-a-day/this.html

I was reading through that, maybe it might help you.
# drinks WAY to much espresso

Micron
Posts: 319
Joined: Sat Feb 16, 2008 1:03 am UTC

Re: What is the Java way to use C++'s version of "this"

Postby Micron » Thu Apr 10, 2008 3:40 am UTC

[quote="ash.gti"]

Code: Select all

public class Someclass {
public Someclass getReferenceToSelf() {
return this
}
}


Why are you trying to do that? Think about what that method actually does. You're calling a method on an object to get the object you called the method on. So you call "foo.getReferenceToSelf()" and it returns the object "foo" which you already have! Now using this to pass a reference to the current object off to a method on some other object might make sense but I can't see any reason to return a self reference that way.

User avatar
Xanthir
My HERO!!!
Posts: 5400
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: What is the Java way to use C++'s version of "this"

Postby Xanthir » Thu Apr 10, 2008 3:47 am UTC

Micron wrote:
ash.gti wrote:

Code: Select all

public class Someclass {
public Someclass getReferenceToSelf() {
return this
}
}


Why are you trying to do that? Think about what that method actually does. You're calling a method on an object to get the object you called the method on. So you call "foo.getReferenceToSelf()" and it returns the object "foo" which you already have! Now using this to pass a reference to the current object off to a method on some other object might make sense but I can't see any reason to return a self reference that way.

This was, in fact, an early entry on thedailywtf.com.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

coppro
Posts: 117
Joined: Mon Feb 04, 2008 6:04 am UTC

Re: What is the Java way to use C++'s version of "this"

Postby coppro » Thu Apr 10, 2008 4:09 am UTC

Micron wrote:
ash.gti wrote:

Code: Select all

public class Someclass {
public Someclass getReferenceToSelf() {
return this
}
}


Why are you trying to do that? Think about what that method actually does. You're calling a method on an object to get the object you called the method on. So you call "foo.getReferenceToSelf()" and it returns the object "foo" which you already have! Now using this to pass a reference to the current object off to a method on some other object might make sense but I can't see any reason to return a self reference that way.
Well, if you have other code in the function body, it can be used for method chaining, which is really useful:

Code: Select all

myObject.setX(3).setY(4).setZ(25);
//Rather than:
myObject.setX(3);
myObject.setY(4);
myObject.setZ(25);
There's examples that don't look so confusing, but I'm too virtuous to come up with one.

User avatar
Gatesunder
Posts: 120
Joined: Sat Feb 09, 2008 11:24 pm UTC
Location: KSU

Re: What is the Java way to use C++'s version of "this"

Postby Gatesunder » Thu Apr 10, 2008 4:19 am UTC

Micron wrote:
ash.gti wrote:

Code: Select all

public class Someclass {
public Someclass getReferenceToSelf() {
return this
}
}


Why are you trying to do that? Think about what that method actually does. You're calling a method on an object to get the object you called the method on. So you call "foo.getReferenceToSelf()" and it returns the object "foo" which you already have! Now using this to pass a reference to the current object off to a method on some other object might make sense but I can't see any reason to return a self reference that way.


Cascading. The best example of which in C++ is in operator overloading, which isn't present in Java. You perform operations with the function, after which the function returns the object so that it's new form may be used in the current expression.

ex. Object1 = Object2 + Object3 + Object4;

EDIT: Bad example because the proper/intuitive use of operator+ returns a temp value, but you can extrapolate to one in which it returns a referance to the current object . . . I just can't think of one off the top of my head

which is equivelent to Object1.operator=( Object2.operator+( Object3.operator+( Object4 ) ) )

I just wanted to pass the current object to another function from inside a function called by the current object.

In any case, After hearing that I could use this in java like this in C++ from you guys (my book didn't help, nor did my web searches), I went back and realized it was a coding error that had caused the problem. Thanks a bunch for the help.
I'm wrong 99% of the time, but some day that 1% will be better than the rest ...

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: What is the Java way to use C++'s version of "this"

Postby Cosmologicon » Thu Apr 10, 2008 4:55 am UTC

Gatesunder wrote:Cascading. The best example of which in C++ is in operator overloading, which isn't present in Java. You perform operations with the function, after which the function returns the object so that it's new form may be used in the current expression.

ex. Object1 = Object2 + Object3 + Object4;

EDIT: Bad example because the proper/intuitive use of operator+ returns a temp value, but you can extrapolate to one in which it returns a referance to the current object . . . I just can't think of one off the top of my head

One familiar example is streams using the << and >> operators.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: What is the Java way to use C++'s version of "this"

Postby Rysto » Thu Apr 10, 2008 1:44 pm UTC

I'm still not understanding why you think that you can't do this is in Java just as well as you can in C++. Neither's type system is really expressive enough to allow you to do method chaining properly(although Java's type system is better than C++'s in this regard), but you can use method chaining in simple cases well enough.

zahlman
Posts: 638
Joined: Wed Jan 30, 2008 5:15 pm UTC

Re: What is the Java way to use C++'s version of "this"

Postby zahlman » Fri Apr 11, 2008 12:40 am UTC

Um, sure you can do it.

Code: Select all

class Thing {
  public int spiffy_thing = 0;

  public Thing do_something_really_neat() {
    spiffy_thing += 1;
    return this;
  }

  public Thing(Thing toCopy) {
    // Notice (a) this is analogous to C++'s Thing(const Thing&), and (b) the fact that we're constructing another object of the same type, instead of a different type, is not important here.
    spiffy_thing = toCopy.spiffy_thing;
  }

  public Thing copySelf() {
    return new Thing(this); // the current object is never actually cloned in memory. We simply allocate a new thing directly, and make it look like a copy of ourself via the constructor.
  }

  public static void main(String[] args) {
    Thing t = new Thing();
    System.out.println(t.do_something_really_neat().do_something_really_neat().copy_self().do_something_really_neat().spiffy_thing);
  }
}
Belial wrote:I once had a series of undocumented and nonstandardized subjective experiences that indicated that anecdotal data is biased and unreliable.

zenten
Posts: 3799
Joined: Fri Jun 22, 2007 7:42 am UTC
Location: Ottawa, Canada

Re: What is the Java way to use C++'s version of "this"

Postby zenten » Fri Apr 11, 2008 3:51 am UTC

Rysto wrote:I'm still not understanding why you think that you can't do this is in Java just as well as you can in C++. Neither's type system is really expressive enough to allow you to do method chaining properly(although Java's type system is better than C++'s in this regard), but you can use method chaining in simple cases well enough.



Gatesunder wrote:In any case, After hearing that I could use this in java like this in C++ from you guys (my book didn't help, nor did my web searches), I went back and realized it was a coding error that had caused the problem. Thanks a bunch for the help.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: What is the Java way to use C++'s version of "this"

Postby Rysto » Fri Apr 11, 2008 4:12 pm UTC

Oh. Carry on, then.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 12 guests