Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

OOP passing arguments

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic