[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Either Java is fucking retarded or I'm fucking retarded.

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 8
Thread images: 1

File: .jpg (8KB, 225x300px) Image search: [Google]
.jpg
8KB, 225x300px
Either Java is fucking retarded or I'm fucking retarded. Can someone help me figure out which one of these statements is true?

I've got a little issue when creating a new Object.
Inside main method:
Rational asd = new Rational(1,2);
Rational qwe = new Rational(15,5);
System.out.println(qwe.converter()+" : "+asd.converter());


Relevant code for class "Rational":
private static int zaehler;
private static int nenner;
public Rational(int z, int n){
zaehler = z;
nenner = n;
}

public double converter(){
return (double)zaehler/(double)nenner;
}


I've created 2 Objects, called 2 functions and tried to printf the return value of the functions. What happens, is when I create the SECOND Object (qwe) it OVERWRITES the first one, so that it printfs "3 : 3". Why?

If I'm missing something completely obvious Im going to kill myself. Tried rubber duck for about half an hour but now I've given up.
>>
>>59398219
i believe its because the variables are static
>>
>>59398252
You are my hero. Seems like I'm the one who is retarded.
Though why exactly does this lead to my error?
I just copypastad shit from google because I obviously don't know what I'm doing yet.
>>
try changing private to public

t. guy who dont know what hes talking about
>>
>>59398297
Nah, that would only lead to Main being able to access zaehler and nenner directly, though Im not doing that in code anyways.
>>
>>59398292
because static makes only one instance of a static field exist even if you create two or more instances of the class it will be shared by all instances.
>>
>>59398328
I was about to ask:
"my variables are static, but my object isn't, so why does it matter what happens to the variables AFTER my Object is being created?"
but then I realized that the method accesses the (static) variables directly.

Damn, you helped me a lot. Thanks again.
>>
This should really be a compiler warning (member method result entirely dependent on static variables), but sadly everything Java compilers do is translating your code as directly as possible to bytecode.
See your javac man page for possible -Xlint options; at least with OpenJDK, all possible ones are already enabled.
Thread posts: 8
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.