+ 2

why this doesn't work with default modifier. however, these classes are in the same package.

public class man { default void sum(int a, int b ){ int sum =a+b; System.out.println("result is "+sum); }} public class main { public static void main(String[] args) { man ma = new man(); ma.sum( (int)7.89, (int)5.73); }}

1st Sep 2020, 12:06 PM
Mahdi (Twelve Imam)
9 Respostas
+ 3
Mahdi Ardini Let me clear you. Actually you are creating a default method inside a class which gives you compile time error. WE CAN'T DECLARE DEFAULT METHOD INSIDE A CLASS. (Please refer the attached code) you may heard about default method inside an interface not in a class. The default feature came in java 8 version. https://code.sololearn.com/cU20fSw26OJ8/?ref=app https://code.sololearn.com/cvZJJbRosCkn/?ref=app
1st Sep 2020, 3:17 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 12
Yes what u have heard is correct,we can use default modifier in the same pakage but we won't specify with the keyword default...it's a default access modifier which can be accessed in the package
1st Sep 2020, 12:46 PM
chaithra Gowda
chaithra Gowda - avatar
+ 11
Yes ,now it's okay with your code! default modifier keyword is used for interfaces There's no provision in the language that allows for this keyword to appear here. It do appear for interface method declarations, but. The new java 8 feature allows an interface to provide an implementation when its labeled with the default keyword
1st Sep 2020, 12:38 PM
chaithra Gowda
chaithra Gowda - avatar
+ 10
We can't access non -static content in the static method ,so it's giving u the error .main method can access only the static content or instance but not non static https://code.sololearn.com/c3wwv5F7cU6k/?ref=app
1st Sep 2020, 12:19 PM
chaithra Gowda
chaithra Gowda - avatar
+ 2
P∆WAN M∆URY∆ thanks P∆WAN i hope you will be sinked in god's blessing
1st Sep 2020, 6:59 PM
Mahdi (Twelve Imam)
+ 2
Mahdi Ardini Thank you so much for your words. Keep learning, keep helping. :)
1st Sep 2020, 7:01 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 1
Chaithra Gowda👑 thanks but i changed the question and corrected the missing braces. can you tell me why this isn't working?
1st Sep 2020, 12:23 PM
Mahdi (Twelve Imam)
+ 1
Chaithra Gowda👑 thanks chaitra but i heard before: that default modifier is used for declaring a method in the same package and different class? is it correct or am i confused?
1st Sep 2020, 12:43 PM
Mahdi (Twelve Imam)
- 1
Aqa Mahdi Shama is the time of Meknedkah from Amosch Dahid
13th Oct 2020, 8:44 PM
Habib Nosrati