A method is just a chunk of code that does a particular job. But methods are set out in a certain way. You have a method header, and a method body. The header is where you tell Java what value type, if any, the method will return (an int value, a double value, a string value, etc). As well as the return type, you need a name for your method, which also goes in the header. You can pass values over to your methods, and these go between a pair of round brackets.
A simple Java method requires a minimum of three items:
Visibility : public, private, protected
Return Type: void, int, double, (etc.)
name: whatever you want to call the method
Visibility means who can access it. If it is public, anyone who has access to your class file can access the method. In some circumstances, this is perfectly fine. If the method is private, only the functions inside of that class can call the method. This is used as utility methods to do something you do not want just anyone who uses the class to do. Protected gives public function to all child classes.
Return type is void if you do not want the method to give you any data back. It would be used for such things as a method that prints out something. Any other return requires a return statement with the type of data it returns. For example, if you add two integers and want the results of that integer, your return type would be int.
Name of the method is anything you choose that is not already used in the class (unless you are overloading the method which is beyond the scope of this article)
If you want the method to do something with the data you supply it, you also need to include parameters within the ( ) You include what data type it is and give that parameter a name (ie: you are declaring a local variable for that method only)
access return type name parameters
public void add (int a, int b)
This would be written
public void add(int a, int b)
// do stuff here
Since the return type is void, you will have to write what you want the method to do inside of the method such as by printing it out from the method
public void add(int a, int b)
- Source Code:
Code that is written in high level language is called source code and in the context of java its called java code and its extension is .java and it’s not directly run on computer compiler is required to convert it in machine code.
BlueJ is an integrated development environment (IDE) for the Java programming language, developed mainly for educational purposes, but also suitable for small-scale software development.
BlueJ was developed to support the learning and teaching of object-oriented programming, and its design differs from other development environments as a result. The main screen graphically shows the class structure of an application under development (in an UML-like diagram), and objects can be interactively created and tested. This interaction facility, combined with a clean, simple user interface, allows easy experimentation with objects under development. Object-oriented concepts (classes, objects, communication through method calls) are represented visually and in its interaction design in the interface.