More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Both belong to the class as a whole and not to the individual objects. Static methods are explicitly called for execution whereas Static block gets executed when the Class gets loaded by the JVM.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, but the overloaded main methods without single String[] argument doesn't get any special status by the JVM. They are just another methods that needs to be called explicitly.
Help us improve. Please let us know the company, where you were asked this question :
Ans. No. compareTo method is declared final for the Enumerations and hence cannot be overriden. This has been intentionally done so that one cannot temper with the sorting order on the Enumeration which is the order in which Enum constants are declared.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It could be worthy to move a method to util class if the method needs to be shared, doesn't require polymorphic behavior and need not be overridden in special cases.
Don't belong to one group through is-a relationship ( You can share through parent class method )
Don't implement a specific interface ( java 8 default methods )
Doesn't involve complex computing as you will be loosing the benefit of object state with just static method.
Doesn't require polymorphic behavior as static methods don't participate in runtime polymorphism.
Help us improve. Please let us know the company, where you were asked this question :
Ans. No. Because java internally treats var args as arrays and hence both method declarations will generate the same byte code and hence would result in ambiguity while determining call binding.
Help us improve. Please let us know the company, where you were asked this question :
Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior.
A static method is not associated with any instance of a class so the concept of overriding for runtime polymorphism using static methods is not applicable.
Help us improve. Please let us know the company, where you were asked this question :
Ans. With Java 8, We can provide method definitions in the Interfaces that gets carried down the classes implementing that interface in case they are not overridden by the Class. Keyword "default" is used to mark the default method.
Ans. Static methods can be called using instance references wherein this would have made sense but static method can also be called using Class name wherein this would mean nothing and hence forbidden.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Earlier any class implementing an interface was supposed to implement all methods declared in an interface. There was no place for optionally implementing all or subset of methods.Though we have abstract classes wherein we could have provided such a mechanism by declaring some methods as abstract while providing definition for some. But as Abstract classes have a body and are comparatively heavier than interfaces and interfaces associate closely to the concept of providing interfacing than abstract classes, Java might have though of providing optional implementation for default methods. This way same interface can be reused in variety of ways rather than making copies of an interface to suit different needs.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Likewise classes, methods also provide a way for code reuse and abstraction. Code is reused, clean and easy to understand if classified properly within classes and methods.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Though var args are used rarely but they are pretty useful if a method is expected to receive variable number of arguments. For example - it's pretty useful for the main method as the caller has the flexibility to provide arguments in infinite ways.It provides a convenience and flexibility to the caller.
Help us improve. Please let us know the company, where you were asked this question :
Ans. No. The whole idea of default method is to provide a default definition in case the implementing class intend to provide definition for only some of the methods. Making any of the interface method private would restrict it from being implemented by the implementing class. So default method is an option to provide implementation and not restricting a new definition.
Help us improve. Please let us know the company, where you were asked this question :
Ans. You can create a non-static local class inside a body of code. Interfaces cannot have local classes, and you cannot create local interfaces.
Local classes are accessible only from the body of the code in which the class is defined. The local classes are completely inaccessible outside the body of the code in which the class is defined.
You can extend a class or implement interfaces while defining a local class.
A local class can access all the variables available in the body of the code in which it is defined. You can pass only final variables to a local inner class.
Help us improve. Please let us know the company, where you were asked this question :