Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OOP passing arguments  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning to pass arguments onto my different classes and got a few things to work. for example :
MAIN

OBJECT / CLASS


Now the above worked, very simple I know, does the job.

What happens if want to by-pass the information from the scanner and do this (I've capitalized the code change only for the purpose of asking the question):


Why doesn't this work? I was watching another tutorial where it worked, but I seemed to have made a mistake somewhere.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to make it static and public to do so which is very bad practice and should not do.

Secondly class name should start from capital..
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused, you said I should make it "static and public" and then you told me not to do it. Did I misread something?
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You added the line: NOISESOBJ.FORCE = 10;

Your Noises object does not as far as I can tell declare a variable called "force"
The only use I see is as a parameter to the slam() method.


What do you expect this line of code to do?
What error message do you get?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... I answered your question that we can do it but after that i told you it is bad practice. See for any problem there are several solutions..

Each solution have some prons and cons. So, security and un-manageability is some of the cons if you use that..
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is some confusion because of the use of capitals for emphasis in the sample code.

Case matters in Java, and by convention variable names with CAPITAL_LETTERS are constants.
Class names should always start with a Capital Letter as well. So your "noises" class should be named "Noises"

These conventions become so ingrained, that when people don't follow them, it makes it hard to read their code :-)
Noises.FORCE by convention should refer to a Class variable of the Noises class called "FORCE"
And to be invoked in that manner from your main method, the FORCE variable would need to be declared as public and static.
public - so that it can be referenced from another class
static - so that it can be referenced without needing an instance of the class.

So that is (I think) where Tushar came from regarding public and static.
And as he pointed out, public static variables are for the most part not a desirable thing.

However, you don't even HAVE a variable called "force" anywhere, so it is a bit of a moot point :-)
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick De wrote:I am confused, you said I should make it "static and public" and then you told me not to do it. Did I misread something?

I think what Tushar is referring to is that you have your driver code in main(). Ideally, main() should have one or two lines to launch the program and no more. We have a page for that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!