简体   繁体   中英

Difference Between Classes And Methods in JAVA

I am learning how to code with Java. What is the difference between a class and a method, and which is which in the following code?

package template;
public class Template {
    /**
     * @param args the command line arguments
     */
    private static class BGCode {
        public BGCode() {
        }
    }
}    

The second question is easy to answer: the things called class are classes. (ie class Template and class BGCode ) There are no actual methods, only a constructor (-> public BGCode ) which is similar to a method but not the same. I don't want to confuse you too much, so here is an article about this topic.

class is a blueprint for an object. classes contain two things:

  1. data

  2. methods (subroutines)

in a method, an action is done on the give input data.

package classes;

public class Example{
static int i=10;
static int j=20;
public static void addmethod(){       //this method is inside Example class
 System.out.println(i+j);        //this method prints i+j;
                 }
public static void main(String[] args) {

      addmethod();               //method is called here.
 }
} 

The difference is grouping (class) versus doing (method). Class is grouping. Method is doing. The method of learning is different in math class than in gym class.

You group class of people, places or things. There is a method by which that group (class) gets things done.

Class-ic example of class is class-room.(puns intended) You have math class, English class, computer class, etc.

One way I like to drive points into my head is to use the absurd. Does this sound absurd ... to say "we have learning classes in math method, English method, computer method." But it's not so absurd to say "we have math testing methods, math learning methods in math class." Right?

In programming, words have exactly the same meaning as they do in the English language. So just look up class and method in any dictionary to get lots of examples. This is true for most programming words in most programming languages.

Here are some other examples. For a vehicle class, a car is a subclass. As are planes and boats. Easy right?

A method is what you do within each class. Like, you drive a car, whereas you fly a plane. You use different methods to drive a car than you do to fly a plane. The method of accelerating a car (by foot) is different than for plane (by hand).

Again, absurding the example ... This doesn't sound right, right? The class of accelerating a car is different than the class of accelerating a plane. And how about this. Cars and plans are methods of vehicles. Argh. Just doesn't sound right, right?

Someone I know internet-searched "what's the difference between Java class and method". This question appeared ranking number two. She was baffled by this question and answers so asked me. I'm stunned too by the rank and by the two down votes for the question and that after five years no one answered it simply. Out of curiosity I searched stackoverflow for "difference between class and method" and this question is still on the first page and really the only question of it's kind.

So I decided to take a shot at helping others who might be baffled by this by answering the first question. Others answered the second question.

Template // Outer Class
BgCode // Nested Class
public BgCode() // Is actually a constructor ( Method which doesn't have any return-type )

This can be an easy answer. Actually a class contains methods. So your question in not in the same context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM