26th Jun 2017, 12:53 AM
esraa ragab
9 Respuestas
+ 1
The any method that you had in your code: public void any() { return this.a; } Had a return type of void. The only allowable return(s) are nothing at all or just a simple return keyword to exit the method early. public void any() { // no return statement at all } or public void any() { if (iWantToExitEarly) { return; // just an empty return statement with no value. } } Any other type such as int in the case of your any() method will produce an error. But this method wasn't even being used so you could delete it all together if you wish.
26th Jun 2017, 2:02 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
There are several issues with this code. For starters your any() and main() methods are outside the class. Then your setVal() method has a return type of int, but returns no value. Your any() method has a return type of void, but returns an int. You can make your int a variable private. Then once you move your methods back into the cal class you will not need to use the this keyword. Your any method currently isn't used so you can get rid of it, unless you're planning on doing something with it in the future. Formatting your code for readability can be very important when it comes to proper placement of your curly braces. Also note that it is the convention that class name begin with a capital letter in Java. public class Cal { private int a; public int setval() { a = 55; if(a > 30) { System .out.println(55); } return a; } public static void main(String[] args) { Cal sa = new Cal(); int g = sa.setval(); System.out.println(g); } }
26th Jun 2017, 2:16 AM
ChaoticDawg
ChaoticDawg - avatar
0
ldo what you say but still error in 16,15,17,18,20line
26th Jun 2017, 1:43 AM
esraa ragab
0
but why you dont use this.
26th Jun 2017, 1:44 AM
esraa ragab
0
and what is wrong about any()asmethod what was the error in it
26th Jun 2017, 1:45 AM
esraa ragab
0
The this keyword isn't needed because the compiler can easily determine the variable you are referring to. If you were passing in a variable to the method that used the same name as the class level variable then there would be a naming conflict between the to and the this keyword could be used to specify the class level variable. int a; public int setval(int a) { this.a = a; // here the 'a' used as a parameter in the method signature shadows the 'int a' from the class, so we use the "this" keyword to differentiate the class level 'a' variable from the 'a' variable that was passed into the method. // this.a refers to the current instances class variable that is declared outside the setVal() method // a without the use of this refers to the variable that is declared in the methods signature. if(this.a > 30) { System .out.println(this.a); } return this.a; }
26th Jun 2017, 2:14 AM
ChaoticDawg
ChaoticDawg - avatar
0
The current version of your code still has the main method outside of the class. The starting point main method must be inside of a class in Java. This is a current copy (as of when I copied it) of your code unmodified: public class Cal { public int a; public int setval() { a= 55; if(a>30) { System .out.println(55); } return a; } } public static void main (String[] args) { cal sa =new cal(); int g = sa.setval(); System .out.println(g); } Notice how difficult it is to tell if you're missing curly braces or if something is misplaced? This is why formatting your code is important. This is the exact same code, but formatted so you can clearly see that the main method is outside your Cal class. public class Cal { public int a; public int setval() { a= 55; if(a>30) { System .out.println(55); } return a; } } public static void main (String[] args) { cal sa =new cal(); int g = sa.setval(); System .out.println(g); } Notice how the closing brace '}' for the class is directly below the p in public class? Notice how the closing curly brace for each method is also directly below its corresponding p? Likewise even the if statements closing brace is directly below the i in if. Visually it is much easier to see that the main method is outside of the class. Move the main method inside the class, reformat your code accordingly, and then fix the capitalization of Cal in the main method; cal sa =new cal(); to Cal sa =new Cal(); and your code will run with the output of: 55 55
26th Jun 2017, 2:43 AM
ChaoticDawg
ChaoticDawg - avatar
0
still error what i should do
26th Jun 2017, 1:06 PM
esraa ragab
0
thank for your help it run now
26th Jun 2017, 1:10 PM
esraa ragab