+ 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); }}
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
+ 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
+ 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
+ 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
+ 2
PâWAN MâURYâ thanks PâWAN i hope you will be sinked in god's blessing
+ 2
Mahdi Ardini Thank you so much for your words. Keep learning, keep helping. :)
+ 1
Chaithra Gowdađ thanks but i changed the question and corrected the missing braces. can you tell me why this isn't working?
+ 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?
- 1
Aqa Mahdi Shama is the time of Meknedkah from Amosch Dahid