Thursday, March 31, 2016

Using Multiple Classes

Huh!! Like a math class isn't it? Ah I don't worry it's over for a while. Let's go for the classes and methods...

  • What's a Java Class?
class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors. Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object.

  • What's a Java Method?
Image result for what's a java methodJava method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console.


I hope you guys got at least a little bit of idea about these Classes & Methods.

  • Create another class. right click project >> New >> Class



  • You can name the class by any name. But for now use "sub_clz"


  • in the sub_clz...
public class sub_clz {

public void sampleMsg(){
System.out.println("This Message from another CLASS");
}

}


In this class we created method [ "public void sampleMsg(){" ] . Inside that method we print [ "This Message from another CLASS" ] message.
  • now run the code
  • Nothing will happen, now go to the main class(used before)
  • Inside the main class we have the main method ( public static void main(String []args) )
  • Only things inside this main method will compile. And also the there must be only one main method.

Calling another class

  •  Now you're in the main method. You have to call the "sub_clz". Simple.... 

                  [  classname objectname = new classname ]  (template)
  • OK we called the sub_clz.
  • Next we're gonna call the method inside the sub_clz.  [ "public void sampleMsg(){" ]
  • This how we do that.
                               [   objectname.method(); ]    (template)




Much more to come, before go for them if you have a problem about Objects just visit
https://docs.oracle.com/javase/tutorial/java/concepts/object.html.

2 comments:

  1. Grazie uomo, è stato salvato il culo

    ReplyDelete
  2. Amigo, por favor, puesto más. Estoy enfermo en Java :) :) :)

    ReplyDelete