Results 1 to 18 of 18
  1. #1
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default declaring classes

    Hello,

    i'm sorry i ask so many questions but i just can't figure out how to do it.
    can someone tell me how to declare a class, i think it has to be done like this.

    Java Code:
    //i think i have to put public in here, so it can be acessed by the program.
    public class car { 
    
    //making fields for the class
    public int weight;
    public int speed;
    
    //i'm adding data into these fields...
    // the tutorial says newValue there, i have to fill in a number in there like 3?
    public void setWeight(int newValue) {
    weight = newValue;
    }
    
    public void setSpeed(int newValue) {
    speed = newValue; 
    }
    }
    is this right?
    and another question, "void" is this a question box?

    Coltragon

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Yep, that's how to do it: when you declare a public class the entire world from everywhere can use your class; if you leave out the word 'public' only other classes in the same package can use your class.

    'void' means 'nothing'; a void method returns nothing, i.e. it doesn't return a value at all. No variable can be of type void because you can't assign any value to it.

    kind regards,

    Jos

  3. #3
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    ok, im going to try it now. hope it works :D

  4. #4
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    compiling went good, when running i get exception in main thread error.
    i think google will solve this problem ;)

    for those who want to know, maybe it can help you or something.
    this is my final code:
    Java Code:
    //i think i have to put public in here, so it can be acessed by the program.
    public class car { 
    
    //making fields for the class
    public int weight;
    public int speed;
    
    //i'm adding data into these fields...
    // the tutorial says newValue there, i have to fill in a number in there like 3?
    public void setWeight(int newValue) {
    weight = newValue;
    }
    
    public void setSpeed(int newValue) {
    speed = newValue; 
    }
    //printing the values
    void printstates() {
    System.out.println("speed of the moment:"+speed+"weight of the car:"+weight);
    }
    }

  5. #5
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    You created two attributes in this class: weight and speed.

    Java Code:
    public int weight;
    public int speed;
    Do you know what the public word means? That any object will be able to modify the value of these two attributes directly.
    This is not something very good. For example, suppose I have another object and this object tries to attribute the value "-1" to weight.

    Java Code:
    public class Numbers{	
    	public static void main(String[] args) {
    		Car car = new Car();
    		car.weight = -1; //it will work
    	}
    }
    It will work because weight is a public attribute and how it has an invalid value.
    To avoid this kind of problem, we generally create private attributes which means that these attributes will only be visible inside the class body. Other objects won't be able to modify their values:

    Java Code:
    private int weight;
    private int speed;
    Now, the class Numbers won't be able to access directly weight and speed values.

    Java Code:
    public class Numbers{	
    	public static void main(String[] args) {
    		Car car = new Car();
    		car.weight = -1; //The field Car.weight is not visible
    	}
    }
    That's why we create set and get methods... (just like the set method you created)

    Java Code:
    public void setWeight(int newValue) {
    	weight = newValue;
    }
    If you pay attention to the code you wrote, the only thing this method do is to give a new value to the weight of the car. Main would look like:

    Java Code:
    public class Numbers{	
    	public static void main(String[] args) {
    		Car car = new Car();
    		car.setWeight(-1); 
    	}
    }
    Now we're able to check if the informed value to weight if valid.

    Java Code:
    public void setWeight(int newValue) {
    	if (newValue > 0) 
    		weight = newValue;
    	else
    		weight = 0;
    }
    That's why we generally don't create public attributes and this is how you're going to use your new method.

    =)
    Please don't laugh at my English... I'm trying my best! :)

  6. #6
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    thanks for the help, the next time i'll create a private.

    when i search the internet for my error
    Java Code:
    exception in thread "main" noClassDefFoundError
    they say i have to set my classpath, but i've already set my classpath to c:\program files\java\jdk1.6.0_17\bin

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by coltragon View Post
    thanks for the help, the next time i'll create a private.

    when i search the internet for my error
    Java Code:
    exception in thread "main" noClassDefFoundError
    they say i have to set my classpath, but i've already set my classpath to c:\program files\java\jdk1.6.0_17\bin
    That directory should be part of your PATH variable, not the CLASSPATH variable; for now go to the directory where YourClass.class is stored and type:

    Java Code:
    java -cp . YourClass
    Java will find your class and run it.

    kind regards,

    Jos

  8. #8
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    that works, but than i get another error

    Java Code:
    NoSuchMethodError
    google tells me i don't have a
    Java Code:
    public static void main(string args[]);
    in my program. where should i add this line?

    (sorry for all these questions)

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by coltragon View Post
    that works, but than i get another error

    Java Code:
    NoSuchMethodError
    google tells me i don't have a
    Java Code:
    public static void main(string args[]);
    in my program. where should i add this line?

    (sorry for all these questions)
    Don't apologize in every reply of yours, everybody has to learn ;-) The game runs like this:

    1) you specify the classpath where a class X is stored.
    2) you start java -cp . X
    3) java loads class X and searches for a public static void main(String[] args) { ... } method.
    4) if found it calls that method and your program has started.
    5) otherwise, Java whines as you have noticed.

    You have to define a main( ... ) method in the class you are trying to run otherwise Java can't start your program.

    kind regards,

    Jos

  10. #10
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    if i add the main class, even if i directly copy it from the sun website.
    i get 13 errors!
    for every word "void" he gives an error.
    and he expects ";" evrywhere... but previously it didn't

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by coltragon View Post
    if i add the main class, even if i directly copy it from the sun website.
    i get 13 errors!
    for every word "void" he gives an error.
    and he expects ";" evrywhere... but previously it didn't
    While some of us are psychic now and then don't rely on it. Post your code; btw you don't have to add a main class, a main method will do. Your errors are just compilation errors, nothing a sharp eye can't solve.

    kind regards,

    Jos
    Last edited by JosAH; 12-21-2009 at 07:09 PM.

  12. #12
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    here's my code
    Java Code:
    //i think i have to put public in here, so it can be acessed by the program.
    public class car { 
    public static void main(String[] args) {
    
    //making fields for the class
    public int weight;
    public int speed;
    
    //i'm adding data into these fields...
    // the tutorial says newValue there, i have to fill in a number in there like 3?
    public void setWeight(int newValue) {
    weight = newValue;
    }
    
    public void setSpeed(int newValue) {
    speed = newValue; 
    }
    //printing the values
    void printstates() {
    System.out.println("speed of the moment:"+speed+"weight of the car:"+weight);
    }
    }

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Look what you did: you mangled all your code; basically you have done this:

    Java Code:
    public class car {
       public static void main(String[] args) {
          // all your code here ...
       }
    }
    It doesn't work like that: your main( ... ) is just a method that gets everything else going; reorganize your code to this:

    Java Code:
    public class car {
       // your original class-defining stuff goes here ...
    
       public static void main(String[] args) {
          // put some car-testing code here ...
       }
    }
    kind regards,

    Jos

  14. #14
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    thanks, alot i made my own java program :D
    i'm so proud of myself now haha ^^

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by coltragon View Post
    thanks, alot i made my own java program :D
    i'm so proud of myself now haha ^^
    Good, now you can crawl you have to learn how to walk and run. Have fun and see you around later.

    kind regards,

    Jos

  16. #16
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    i was seeing this more as saying "mama" than crawl :P

    @mods this can be closed now

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by coltragon View Post
    i was seeing this more as saying "mama" than crawl :P

    @mods this can be closed now
    Most toddlers can crawl before they can speak ;-P

    kind regards,

    Jos o&o

  18. #18
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    oops forgot about that, i thought it was in the other way around, speak before crawl -_-

Similar Threads

  1. Declaring URL object
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 08-03-2009, 08:05 PM
  2. Declaring variables
    By Flashmatix in forum New To Java
    Replies: 12
    Last Post: 01-20-2009, 04:43 PM
  3. Declaring an ArrayList
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 08:36 PM
  4. Declaring Vector
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 09:14 PM
  5. Declaring Interface
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 09:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •