+ 1
Difference between method and class in java?
Method vs Class
10 Respostas
+ 1
- You can call static method and static variable without creating the object of class But In case of instance method and instance variable you have to create object for accessing or performing certain task.
- Static method and static variable use static keyword whereas instance method and instance variable don't have any special keyword.
- for example
static int var; //(static variable)
static void func(); //(static method)
int var; //(instance variable)
void func(); //(instance method)
- Another best example of static method is main method in java.
//public static void main (String args []){}
+ 1
The main difference between Class and Method is that class is a blueprint or a template to create objects while method is a function that describes the behavior of an object.
A programming paradigm is a style that explains the way of organizing the elements of a program. Object-Oriented Programming (OOP) is a common software development paradigm. The developers can model the software into a set of objects and these objects communicate with each other by passing messages. Class and method are two concepts related to OOP. Moreover, a method is written inside a class.
What is Class
A class is a template that helps to create single or multiple objects. It is not possible to create objects without a class. For instance, to create Student objects, there should be a Student class. A class contains attributes and methods. The attributes explain the states an object should have whereas the methods describe the behaviors or the functionalities the object should have. A class also has a special method called a constructor to give initial values to the attributes.
The attributes and methods of a class are members of a class. They can have visibilities such as public, private and protected. Overall a class is a logical entity to create objects while an object is a physical entity.
What is Method
A class consists of methods. A method is a function that is used to describe the behavior of a function. Moreover, it helps in code optimization and achieving code reusability. The programmer can make the method public, private or protected depending on the application. Public methods are accessible outside the class while protected methods are accessible within the class and by subclasses. Furthermore, private methods are accessible within the class. For example, assume a banking application. It has behaviors or functionalities such as transfer, withdraw and deposit money. Methods denote these behaviors.
+ 1
đđąđąđđš đđĄđđČđđ„ ,Jnn andJames Clark I. Vinarao thanksđđ
Now i gain knowledge about class &methods and how to utilise static method and how to identify the static method
Again thanks for all....for your guidance....đ
0
A class can have methods and you can create objects of a class. If the method is static you dont need to create an object to call that method because its a class method and you only need the class to call it. If it is not static you need to create an object of that class first with the constructor method of that class, which is also a method but is named like the class name and has no defined return type.
0
đđąđąđđš đđĄđđČđđ„ and Jnn thanks now I understood what is difference method and class and I now know how to utilise the static method.....
But please tell me how I know whether the method from the particular class is static or not?
0
Example:
public class Animal {
// object attribute
private String name;
// entry point of program
public static main(String [] args){
Animal a = new Animal("Tim");
System.out.println(a.getName());
}
// constructor
public Animal(String name){
// initiate attribute
this.name = name;
}
/* method which can only be called by an object of Animal because the name attribute needs to be initiated */
public String getName(){
return name;
}
}
0
Class is nothing but which contains the declarations of variables,functions,methods,object creation all these type of operations will comes under the class
For examples a car is class which has seat , steering,lights ,parts all these parts comes under the class of car.
Method:It is the but main logic of the given code i.e what the operation to be performed all these are placed under the method .
For example in a car engine is the method because it has to perform the operation for moving the car .
0
Everything in Java is stored in a class (Not including imports and packages). Methods are code that can be called at anytime in that class. For example, if you had a class of the name "App" and a method named "action" then here is the code:
class App {
public static void action() {
//Code here
}
}
You can use this method by using method name and parentheses. In this case, the method is action so we will be calling that by doing "action();." If you had an external class that you are calling this in, then put the class name, a dot and a pair of parentheses. If you had a class called main and you want to call this method from class App the do this:
class Main {
public static void main(String[] args) { //Main method
App.action();
}
}
I hope this helps you do what you need.