• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.out constructor (this.) values  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write System.out.println (  );
And console to show me 'w' and 'q' elements. However I dont know what to type in. Maybe extra object/method needed, I dont know.

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, w and q are static, so you should not be using this.w or this.q, just "w" and "q".  But if you want the assignments in mets() to affect the S.o.p in line 17, call the method before that line.

[Edit: the "not" was originally missing.]
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:First of all, w and q are static, so you should NOT be using this.w or this.q, just "w" and "q".


I think that this is what you intended to say.
 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java mostly works in a line by line unless you call methods that are in a different area of your source code(class file). What you are doing is trying to print a value from a variable that has not yet been assigned a value . try puting mets(); on line 16 this will help you.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:
Knute Snortum wrote:First of all, w and q are static, so you should NOT be using this.w or this.q, just "w" and "q".


I think that this is what you intended to say.

Yes, thank you.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray mann wrote:java mostly works in a line by line unless you call methods that are in a different area of your source code(class file). What you are doing is trying to print a value from a variable that has not yet been assigned a value . try puting mets(); on line 16 this will help you.

No,no.
I was there playing with constructors and this. to find out how it works.

I specially declared w and q outside "running method".
I have readen/heard that constructors often serve instances, whos about to sending variables to all classes.

Maybe the problem is, that met () {} is actually method, and not constructor.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't user static variables unless you have a compelling reason to do so.

iWhen you have instance variables, you do not need to use "this" unless you also have a local variable in the method with the same name.
For example,

This code will print:
  5
  0
The first line is "5" because it is using the local variable i1 declared in the calling sequence, which hides the instance variable named i1.
To get the instance variable, you need to use "this" in order to distinguish it from the local variable.
However, if there is no local variable hiding the instance name, the "this" keyword is not needed:

this will print zero for both lines, since here i1 and this.i1 are the same thing.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:you do not need to use "this" unless you also have a local variable in the method with the same name.

But it also fine to use the this. prefix. In fact, some people -- myself included -- use it all the time for instance variables so that the code is consistent throughout and the meaning clear.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . .


Error: Could not find or load main class paketas2.Tests2
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only push the quote button when you are answering a particular point in an old post and don't quote the whole post. The error is unrelated to the previous post. In order to help you with that error, we shall need lost of details, including the folder structure you are working in, and which .class files are in which folders. You do not appear to have provided any details of the Tests2 class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!