Can void method be defined using dot notation? [duplicate] - java

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Closed 5 years ago.
I've just started learning Java and I am stuck at this MCQ:
Assume class Temp is defined as belowand the statment Temp a= new temp() is
successfully executed Which of the statement is illegal in Java?
class Temp {
public static int i;
public void method1() { }
public static void method2() { }
}
A. System.out.println(i);
B. Temp.method1();
C. a.method1();
D. Temp.method2();
The answer is B, but I can't understand why. Is it because a void method cannot be defined using the dot notation unless it's static?

Because method1 is non-static method. You can use methods with class names only if they are static. Look here for more details.

Related

Java Enum why static line executes only once? [duplicate]

This question already has answers here:
What is the difference between a static and a non-static initialization code block
(9 answers)
Closed 4 years ago.
The result is:
1
3
1
3
1
3
2
The constructor runs for A,B and for C (3 times). But if you use static keyword it runs only once. What is the reason of this? And why does this line executes last?
enum Enums {
A, B, C;
{
System.out.println(1);
}
static {
System.out.println(2);
}
private Enums() {
System.out.println(3);
}
}
public class MainClass{
public static void main(String[] args) {
Enum en = Enums.C;
}
}
There are three different things at play here:
private Enums()
{
System.out.println(3);
}
This is the constructor. It (or some other potential constructor) runs when an instance is created.
{
System.out.println(1);
}
This is an instance initializer. It runs when an object is being constructed, regardless of which constructor is used.
static
{
System.out.println(2);
}
This is a static initializer. It gets run once this class has been loaded into the JVM, whether or not an instance is being created. However, since we're dealing with an enum, all its instances have to be created as part of loading the class. That's why it runs after the three constructors and initializers.
To complement #smallhacker answer you should read the answers in Static initializer in Java
Uff! what is static initializer?
The static initializer is a static {} block of code inside java class,
and run only one time before the constructor or main method is called.

Java polymorphism when passing variables. [duplicate]

This question already has answers here:
What is the difference between method overloading and overriding? [duplicate]
(2 answers)
Closed 4 years ago.
class X{
public void print(X x){System.out.println("xx");}
public void print(Y y){System.out.println("xy");}
}
class Y extends X{
public void print(X x){System.out.println("yx");}
public void print(Y y){System.out.println("yy");}
public static void main(String[] args){
X x = new Y();
x.print(x);
System.out.println(x.getClass());
}
}
The output i get is "yx" end I dont understand why, x.getClass() returns "class Y" so shouldn't its call the method where the parameter is Y?
Overloads (i.e. choice which of multiple methods of the same class/interface with the same name to use) are resolved based on static types. This is in opposition to overriding (choice between X#print(X) and Y#print(X) which is resolved at runtime.
Dynamic binding doesn't work for method parameters, hence parameter resolution of method is done in compile time, and the compiler resolves with the static type X.

Can anyone describe what is the correct method of dynamic method dispatch of following 2 programmes [duplicate]

This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 5 years ago.
class B{
public void p(double i){
System.out.println("B");
}
}
class A extends B{
public void p(double i){
System.out.println("A");
}
}
class Demo{
public static void main(String args[]){
//way of using polymorphic 1
A a=new A();
a.p(10.0);
//way of using polymorphic 2
A a=new B();
a.p(10.0)
}
}
Both 2 ways provide same answer.But I want to know what is the difference between these 2 ways of using dynamic polymorphic.what is the best way to use in a program?
From the context and intent of polymorphism Your second way makes more sense.
If a method is overridden then in the run time the method of object type will be executed. Not the reference type.

Inheritance issue : Results are different on creating same class object [duplicate]

This question already has answers here:
Does polymorphism apply on class attributes in Java?
(8 answers)
Closed 5 years ago.
Case 1:
In the below example if i call a.i it is printing 10 as answer. But A a = new B(); isn"t this like object of b is getting created so value 20 should be printed instead of 10.
class A
{
int i = 10;
}
class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}
Case 2:
Also if i create the same program as above using methods inside classes instead of printing variable values as in above case then result is different :
class A{
void test(){
System.out.println("hi");
}
}
class B extends A{
void test(){
System.out.println("hello");
}
}
public class Test {
public static void main(String[] args) {
A a=new B();
System.out.println(a.test);
}
Now in this case hi is printed instead of hello , so why is the behavior different when i try to print variable value and when using methods? Overiding happens between the methods only and not with variables?
Because what you have in the first example has nothing to do with polymorphism as fields read are not dynamically dispatched.
What you have however is a name shadowing, so i in the statement A.i refers to the field declared in A and B.i is invisible at this point.
In Java only methods can be overriden, not instance variables.
When you declare a field with the same name as an existing field in a superclass, the new field hides the existing field. The existing field from the superclass is still present in the subclass, and can even be used
Check these -
Java Tutorial - Hiding Fields
JLS Example - Hiding of instance fields

Execute certain methods on object creation from constructor [duplicate]

This question already has answers here:
Can I call methods in constructor in Java?
(7 answers)
Closed 7 years ago.
Alright, I think this is a faily simple question, but I just can't wrap my head around this.
Lets say I have this pseudo classes with their respective functionalities. Can I call the methods from within the constructor itself, so it launches on object creation?
Class One
public class Apples{
public String a;
public String b;
Apples(String a, String b){
this.a = a;
this.b = b;
specificMethod();
}
public void randomMethod(){
System.out.println(this.a)
}
public void specificMethod(){
System.out.println(this.b)
}
}
Class Two
public class Oranges{
Apples green = new Apples(a,b)
}
Yes, if you put a method in an object constructor which is called it will run the methods inside the constructor.
Yes.
Many people will even just call an _init function instead of doing everything inside the constructor. That way you can reinitialize an object without creating a new one.

Categories

Resources