[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 33 KB, 301x301, java_starting.jpg [View same] [iqdb] [saucenao] [google]
2509014 No.2509014 [Reply] [Original]

Hey /sci/. In java, is there a way to reference an object that just invoked a method? I'm trying to do something like:

System.out.println(Shape + " called paint()");

you see what I mean?

The most success I've had is having it print out "shapeName@memoryLocation called paint()"

>> No.2509019

inb4 all the java hate. We gotta learn somewhere and I heard it's best to go from java to C++. Simpler to more in-depth.

>> No.2509023

I have a feeling some sort of toString is needed, but I've never used it so I'm really not sure.

>> No.2509024

With action events, there is a method in the event object that lets you get the calling object. For non-awt stuff, I don't know of a way that doesn't involve deep black magic.

>> No.2509029

>>2509024
Actually, just thought of a way. Print out the stack trace. Put in a try{} block, throw an IOException e, then call e.printStackTrace().

>> No.2509033

>>2509024
Damn. I've seen action events mentioned, but my book doesn't talk about them for a couple more chapters.

I might be thinking about it too hard. My output:

Circle@48bd6a5f called draw()
Rectangle@b92a848 called draw()
Polygon@496614e7 called draw()

When I just want it to read

Circle called draw() and so on.

I'm using this book that came with a compiler and going through the exercises, but the explanations and index is shite.

>> No.2509035

Give the object a toString() method, it will override the default string output to whatever that method returns.

>> No.2509046

>>2509035
When I try shape.toString() +" " it gives me the same output as >>2509033.

this.toString() and super.toString() does the same.

(I'm toying with inheritance. A Shape superclass with fields with some types of shape subclasses using them. My print statement is supposed to be in my Shape parent class.)

>> No.2509051

>>2509029
That's... a lot of stuff I don't recognize sadly. Sorry. Thanks for the idea through; I'll save it to a file and try it out when I start seeing through terms to see if it works.

>> No.2509055

java is poop

>> No.2509062

>>2509046
If you implemented it right you should only have to do println(shape + "whatever"). There should be a custom toString() method in each subclass, so for a circle it would be:

public String toString() { return "Circle"}

If you have it set up like that already then I couldn't tell you why it doesn't work. It's been a while since I've written anything in Java...

>> No.2509067

>>2509062
Oh god, I can't believe it was that simple.

You were right, thanks very much.

>> No.2509070
File: 4 KB, 60x108, java oh pshhtt.png [View same] [iqdb] [saucenao] [google]
2509070

>>2509055

>> No.2509075
File: 484 KB, 500x282, HELLO MEGAN.gif [View same] [iqdb] [saucenao] [google]
2509075

>>2509067

>> No.2509254

beginner java thread?

i'm calling a method in a main method, but it won't compile because "non-static method cannot be referenced from a static context."

how can i work around that? i tried to create a helper method but i don't think i'm doing it right because i got the same error.

>> No.2509279

>>2509254

main is static, so make the other method static (or call it in an instance of an object)

>> No.2509283

>>2509254
haaaalp

System.out.println(s + super.toString());

doesn't work. also tried making it a field.

private String info = super.toString();
and
System.out.println(s + info);

with the same error.

super.toString() hates me. ;_;

>> No.2509296

>>2509283

you're using super in a main method?

>> No.2509301

>>2509296
not anymore, i've moved it to another method. but if i don't make that static too my code blows up.

>> No.2509335

>>2509283
>>2509301
D:

>> No.2509351

>>2509254

use public static for every variable outside a method to create global variables.

>> No.2509377

>>2509351
hey cool, that's doing something different now. nearly there i think. It's compiling but the output it null.

public static String info;

public String toString()
{
String s = "type =" + variable;
info = s;
return s;
}

>> No.2509382
File: 183 KB, 566x690, 1280786169585.jpg [View same] [iqdb] [saucenao] [google]
2509382

>java to C++.
impossible, once you're ruined for life in java/basic you can learn real programing

>> No.2509388

cout<<method();

Not everything needs to be a fucking class faggots

>> No.2509393

>>2509382
Baaawwwww

Get your elitist shit out of here.

>> No.2509397

>>2509388
wut

>> No.2509403

>>2509377
>>2509351
tightened that up, still null though.

public static String info;

public String toString()
{
info = "type =" + variable;
return info;
}

>> No.2509411

>>2509377

learn how to use parameters


TBH just post your code in pastebin or something

I'm watching porn and I have no time to analyze your problem based on your description, just post the entire code and I'm going to fix it

>> No.2509456

>>2509411
i actually have 5 classes with inheritance relationships and whatnot. i was trying to be brief because it could quickly get complicated.

basically i have an ArrayList of objects, which were created from separate classes and are getting their fields from a Superclass.

i fixed those classes' toString() methods so it just prints out the name of the class (rather than the location).

i then had this:

public static void printTypes(ArrayList l)
{
System.out.println("PRINTING...");
for(Object o : l)
{
String s = o.toString();
System.out.println(s);
}
}

which worked for some reason. i then wanted to print out System.out.println(s + super.toString());

but that's not working. right now i'm trying to instead print out (s + info), with info coming from my Superclass.

which is where >>2509403 comes in.

my "public static String info;" is reading as null unless I have my super's toString() run in the constructor.

but that doesn't do me much good from the other class.

System.out.println(s + super.info) right now prints out my class name, and "null." which i'm trying to fix.


does that help, or give you any ideas?

i really appreciate any help.

>> No.2509465

>>2509456
System.out.println(s + info) rather, gives me the class name and null.

>> No.2509483

>>2509456
>>2509465
to sum up, i guess i need to know how to pass an object into toString()?

>> No.2509496

>>2509456
WUT

>> No.2509497

Make a new Shape class which is identical to the previous one except for that it's paint method also prints the name of the object.

>> No.2509506

Sup. I have 3 Java labs due last week. Not gonna ask questions though. Just going to beat my head against my desk cause that's how I roll.

>> No.2509520

>>2509456
>>2509465
>>2509483
scared off my help. ;_;