• Post Reply Bookmark Topic Watch Topic
  • New Topic

declaring methods in same class?  RSS feed

 
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please tell me why my program won't compile,If I take out the second(); method call from the main method it will work until I insert it again and it gives me errors,am i declaring the method wrong,please help I'm so confused (also I tried calling the first(); method and to no avail either





class ut{

public static void main(String[] args){


second();



}


public void first(){

second(20);

}


public void second(int age){

System.out.print(age);

}



}
 
Ranch Hand
Posts: 119
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it:



You need change the signature of your methods for a static.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

also would you know how I could assign that variable in the second(int age); parameters using the keyboard.class means as user input
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't make things static so they will compile. Far better to create an object:We even have an FAQ about it. Please have a look at conventions about naming in Java.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote: . . . the keyboard.class means as user input
What keyboard class?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keyboard.class its a way you can get user input we use it in university

after you put the keyboard.class into the same folder you can use the code for example

age = Keyboard.readInt();

but the scanner method would be also great if anybody could explain to me how that would work
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Keyboard class may use a Scanner under the bonnet; I can't tell. When I started, Scanner hadn't been developed, and we often wrote our own keyboard input classes. I still think it is a good idea to write your own utility class for keyboard input. You can get the input via the keyboard like this (or similar)Note you don't use the first() or scecond() methods in that example.
More about utility classes later.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find the second line in the main method easier to understand if I expand it a bit:-Utility classes have some strange features:-
  • They exist to provide services for other classes' objects, rather than being objects themselves.
  • They have private constructors, never called inside the class, for reasons explained here.
  • They have no instance (=non‑static) members.
  • They have static fields.
  • Their fields are often public static final ... and used as constants.
  • Their methods are all static, taking all information in via their parameters and returning any information via the returned value.
  • They often have plural names, e.g. Arrays, Collections. Not Array or Collection.
  • Explore the Math class or Arrays class with that info in mind.

    Now you have that information, you can create a KeyboardInputs class. Give it some methods and a (private) Scanner field. Use the methods to get various types of input from that Scanner. You can enhance the methods by passing a message to request input, or insisting input be within a particular range, etc. Read all about Scanner. And I shall tell you exactly what to do. No, I won't. Read this, and this. Beware of nextLine.
    Now, I think you have enough information to write your own utility class; you can keep it for ever for keyboard input. If you are multi‑threading, you will need to synchronise all methods, at least, however.
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks very informative
     
    Campbell Ritchie
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
    And look forward to seeing the completed article.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!