Posts Tagged creating methods

Java Tutorial 6 – Multiple class methods

If u want to make more complex and big application You should divide your whole code into different classes.

for example if you are creating a Calculator you can have a class that holds main method and another class which contains all operations like add, sub, multiply etc. and when dealing with GUI you can also have one more class for defining all the GUI elements like buttons, Textfields etc.

(maan plz stop talking…)

  1. Create 2 classes whatever you want.
  2. create main method in one class.
  3. create methods that you want to use in other class.

Like this way

Class1:

Class 2:

  • We have created 2 classes one has methods and other will use it.
  • class 1 has main method that will be run first and class 2 has methods that we will use in class 1
  • class 2 has 2 methods boy() and girl()
  • Now to access method of class2 we created object of class2 in class 1 that is c2object.
  • now only we have to do is to refer method of class 2 bye calling object.method() means if u want to use girl() then c2object.girl().
  • you can name object anything you want.
  • if you have an int in class2, you can also use it by referring c2object.intname

Practice more and implement different logic to improve you speed and accuracy.

Have a nice day, Peace.

, , , , ,

1 Comment

Java Tutorial 5 – Creating methods

We have used system defined methods like println() and nextInt(), Now let’s create our own method and use it.

Defining method is very useful when bunch of code is getting repeated,  just put that code in a method and just call that method instead of writing all the code again

create a class and add this code as always we do.

  1. Why int and Scanner before main method? 

    if we create int inside the main method, then, only main method can use or change that int. so defining anything in the class and outside any method is called Global declaration.

  2. Why Static?
    to use that int into the main or any other method because main itself is static so any variable used within it must be static and thats why we also crated all the methods static.

We created 3 methods and it can be defined anywhere in the class.Observe the out put and you will dig out everything.

u can create different methods of adding subtracting etc. and create  calc.

Doubts invited,

Have a nice day, Peace.

, , , , , , ,

Leave a comment

Design a site like this with WordPress.com
Get started