In this example , the Student class has three attributes that correspond to a student's name, age, and grade: name, age, and grade.getName(), setName(String name), setAge(int age), getGrade(), andsetGrade(double grade)are among the class's other methods. The get methods are used ...
Example 1: // Java program to read a byte// from the userimportjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){bytebyteVal=0;Scanner SC=newScanner(System.in);System.out.print("Enter byte value: ");byteVal=SC.nextByte();System.out.print("Byte value is: "+byteVal)...
Example 1: Java Methods class Main { // create a method public int addNumbers(int a, int b) { int sum = a + b; // return value return sum; } public static void main(String[] args) { int num1 = 25; int num2 = 15; // create an object of Main Main obj = new Main();...
publicclassClassgetDeclaredFieldsExample2 { publicstaticvoidmain(String[] args) { try{ ClassgetDeclaredFieldsExample2 obj =newClassgetDeclaredFieldsExample2(23); Class class1 = obj.getClass(); Field[] getfields = class1.getDeclaredFields(); for(inti =0; i < getfields.length; i++) { Sys...
3. Java Abstract Keyword Example Let’s see an example ofabstractkeyword. In given example, we have anabstract classAnimalwhich has oneabstract methodmakeNoise(). This class is inherited by two child classes i.e.DogandCat. Both classes implement the methodmakeNoise()according to their nature....
Program Example of Method Let us take an example to show the method definition in the class. Step 1:First we create a classMethodDemoin which we define methods: class MethodDemo { int length,breadth; void getData(int l,int b) //method definition ...
An abstract class can have both the regular methods and abstract methods. For example, abstract class Language { // abstract method abstract void method1(); // regular method void method2() { System.out.println("This is regular method"); } } To know about the non-abstract methods, visit...
publicclassJavaExample{publicstaticvoidmain(String[]args){Stringstr="Welcome";Stringstr2="Home";System.out.println(str.concat(" ").concat(str2));}} Output: WelcomeHome Java String Methods Here are the list of the methods available in the Java String class. These methods are explained in th...
Class class getDeclaringClass() method: Here, we are going to learn about the getDeclaringClass() method of Class class with its syntax and example.
Access Methods With an Object Example Create a Car object namedmyCar. Call thefullThrottle()andspeed()methods on themyCarobject, and run the program: // Create a Main classpublicclassMain{// Create a fullThrottle() methodpublicvoidfullThrottle(){System.out.println("The car is going as fas...