In Java, Differencebetween This.Method() and Method()

In Java, what is the difference between this.method() and method()?

The only time it matters is if you are using OuterClass.this.method() e.g.

class OuterClass {
void method() { }

class InnerClass {
void method() {
OuterClass.this.method(); // not the same as method().
}
}
}

What's the difference between a method and a function?

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:

  1. A method is implicitly passed the object on which it was called.
  2. A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).

(this is a simplified explanation, ignoring issues of scope etc.)

Difference Between Classes And Methods in JAVA

The second question is easy to answer: the things called class are classes. (i.e. 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.

What is the difference between the following two methods in Java

There is no difference between these methods: the else in the first method can be safely deleted, because once the if branch is taken, the execution leaves the method unconditionally.

There are companies that prefer one of the two variants in order to achieve consistency among the code written by large groups of developers.

difference between a getter method and a method which returns the state of an instance variable?

One form isn't using JavaBeans conventions. Functionally speaking, systems that expect you to follow those conventions will not work or be very cumbersome to set up if you use non-conventional getters/setters for your beans, but if you're not, then there's no real difference.

Since Guava has a lot of collections, and the Collection interface actually defines a size() method, my gut tells me that Guava is more inclined to follow the Collection interface than JavaBeans conventions.

Difference between synchronized(this) and few synchronized methods of class

No, they are the same.

private synchronized void foo() {}

private void foo2() {
synchronized(this){
}
}

They will do the exact same as both monitor the instance which they are called from.

A good tutorial can be found in the blog of Jakob Jenkov
http://tutorials.jenkov.com/java-concurrency/synchronized.html#java-synchronized-example

Happy coding!

Java inheritance: What is the difference between method overriding and method hiding?

Hiding it means that you can't call super.method() in the sub class's implementation.

So for example

class Cat extends Animal {
public static void testClassMethod() {
super.testClassMethod(); //this is not possible
System.out.println("The static method in Cat");
}
public void testInstanceMethod() {
super.testInstanceMethod(); //this is fine
System.out.println("The instance method in Cat");
}

}


Related Topics



Leave a reply



Submit