CAN SOMEONE EXPLAIN THE CLASS TO ME I NEVER UNDERSTOOD IT

-1 Abdirasaq Ali · September 16, 2014
CAN SOMEONE EXPLAIN THE CLASS TO ME I NEVER UNDERSTOOD IT 
My teacher all ways sayed it's like a driver. 
I still don't under stand and i want to use other class beside Public static void main(String []args) 
or should I say methods? 

Post a Reply

Replies

Oldest  Newest  Rating
0 Ethan Fraser · September 16, 2014
I'm not sure what sort of driver your teacher meant but I think you're talking about methods, not classes. When you open Eclipse and create a class then your main method (assuming your class is named Apples) it would look like this in the text editor of Eclipse:
package insert.The.Path.thatIsYourPackage;

public class Apples {

public static void main(String[] args) {
System.out.println("This is your main method, it is executed when this class runs");
}

}

Now a method can do anything from open a JFrame, design a JFrame, make a sound, take a picture and so on. It's what you call when you want to do something, all you have to do is make that method that does that something. the main(String[] args) method is the method that is run when the class is run, the method doesn't have to be in a specific class but the moment that class is run, so will the main method, no other method runs unless called. Classes can be used to store different data, things like methods, GUIs, data processing blah blah blah can be put in separate classes. It's a good idea to use separate classes as using one class will just create a 100 line program with 101 errors if you get my meaning. It's just tidier. Any other questions or I didn't explain it thoroughly enough, just ask :)
+1 Savad Dekrup · September 16, 2014
Here is how I see classes as:

Class is like a holder of functions.

public class myClass {
   //Functions, Variables.
}

public class myAnotherClass {
   //Functions, Variables.
}


Resource: My experiences.
0 Abdirasaq Ali · September 16, 2014
I mean like get set and stuff my teacher sayed the first thing is making ur variables private 
public class Fridge{
private int banna; 
private int cookies; 

public  Fridge (int B, in C)
{
 banna = B;
cookies = C;
B = 200;
C = 500; 

}

public void EatFood ()
{
  System.out.println("I ate 20 Banna and 100 cookies"):
}


What do I do after get Get Set??

}
0 Patrick Lehmann · September 16, 2014

public class ExampleClass {
  //These are your two fields
  private int bananas;
  private int apples;

  //This is your setter for Bananas. With that you can set the ammount of it
  public void setBananas(int ammount) {
    this.bananas=ammount;
  }

  //This is your setter for Apples. With that you can set the ammount of it  
  public void setApples(int ammount) {
    this.apples = ammount;
  }

  //This is your getter for Bananas. That way you can get the ammount of it
  public int getBananas() {
    return this.bananas;
  }

  //This is your getter for Apples. That way you can get the ammount of it
  public int getApples() {
    return this.apples;
  }
}


In another class you then do

public class AnotherClass {
  public static main(String args[]) {
    // Create an object of the other class
    ExampleClass exampleClass = new ExampleClass();
    
    //Calling the setter methods
    exampleClass.setApples(5):
    exampleClass.setBananas(20);
    
    // This prints out "There are 5 Apples and 20 Bananas"
    System.out.println("There are " + exampleClass.getApples() + " Apples and " + exampleClass.getBananas() + " Bananas");
  }
}
0 Satan's Mayhem · September 18, 2014
@Patrick Lehamn what does pulbic static main(String args[]) do?
+1 Abdirasaq Ali · September 18, 2014
Satan It runs the code it compiles and runs the code it's the main thing compare to all the other methods
0 Çağlan Turgut · September 18, 2014
@Satan's Mayhem

The compiler searches all your code and finds that main method and starts from there.. 

As for String args[]:

http://stackoverflow.com/questions/890966/what-is-string-args-in-java

Also it is not necessarily need to be named as "args". It is just how most programmers do. Like passing a line after each semi-colon.
0 Developer John · September 19, 2014
Remember: 

Classes are holders for code, && methods are collections of statements to perform an operation. For an example, a store is a class, but the methods would be like the certain jobs required to run the store.
  • 1

Java / Android Development

124,519 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator