I accidentally wrote a code differently than what I should've, and I got these errors :
"Illegal modifier for parameter a; only final is permitted" "Illegal modifier for parameter b; only final is permitted" "Illegal modifier for parameter c; only final is permitted"
The code that I wrote and gave these errors:
Java Code:
class Math { public static void main(String[] args) { static int a = 11; static int b = 35; static int c = 29; //the rest of the code below
[Code] ....
I noticed that I can declare "static int" only under "class Math" and not under "public static void main".(I had to remove "static" if declaring int under "public static void main");
class Course { String courseName; } class Entry { public static void main(String[] args) { Course c = new Course(); c.courseName="java"; System.out.println(c.courseName); } }
I have defined these two classes under same java project in Eclipse IDE with no package. Above two class are having default classes. class Course is also having a instance variable courseName with default access. So when we try to compile the Entry class it will give the errors while accessing the instance variable courseName on Line 6 and 7. As both the classes are having default access modifier. class Course is not visible to class Entry, then why we do not get any compilation error while creating the object of class Course on line 5?
Being new to java I am a bit lost as to why my session attribute for this banking app wont add the deposits to the session var... it just keeps going back to the amount I set it to originally - so if I set the beginning balance to 3000 then deposit 100 it becomes 3100, but if I then try deposit another amount eg. a extra 200 it becomes 3200 not 3300 like it should be !!
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.net.*; import java.text.DecimalFormat; public class SessionBank extends HttpServlet
I am getting the above error and am not sure which direction to proceed. In the class, the only way we have discussed getting user input is by System.in.read. I have searched and apparently found better methods for getting user input, but wanted to stick with what has been presented thus far.
Assignment Directions:
1. Create a new class named “valuemethod”
2. Create a new method named “Main”
3. In Main Write the code that will call the method EnterPay and YearlySal
4. Create a new method named “EnterPay”
5. In the EnterPay method Write the code that asks the user to input their hourly wage. Use the formula to calculate their yearly salary: wage * 2040. Return the yearly salary to the main method
6. In the main method write the code that will display this: “Your yearly salary is:
Java Code:
package valuemethod; public class Valuemethod { public static void main(String[] args) //throws java.io.IOException -- moved to EnterPay
The 2 minute drill from page 69 SCJP kathy and bert book, says regarding Interfaces, that - "A legal nonabstract implementing class must not declare any new checked exceptions for an implementation method."
When I try the below given code in eclipse , it does not throw any errors . (Here I have tried to throw NullPointerException from testFunc whereas the interface function throws IllegalStateExc)
package abstracttesting; public class StaticCheck implements check{ public void testFunc() throws NullPointerException{ // TODO Auto-generated method stub } public static void main(String[] args) { // TODO Auto-generated method stub } } interface check{ void testFunc() throws IllegalStateException; }
"A constructor cannot be abstract, static, final, native, or synchronized."
I understand on why it can't be all of the above, except "final".
Why can't we have a final constructor, i understand constructors are not inherited, hence no chance/case of overriding etc. But why is it not allowed at all ?
I was practicing my java skills and came across an exercise in which a non parameter constructor calls a two parameter constructor. I tried a few searches online but they all came back unsuccessful. This is the part I am working on:
public PairOfDice(int val1, int val2) { // Constructor. Creates a pair of dice that // are initially showing the values val1 and val2. die1 = val1; // Assign specified values die2 = val2; // to the instance variables. } public PairOfDice() { // Constructor that calls two parameter constructor }
I tried calling the two constructor using the line "this(val1, val2)" but I get an error because val1 and val2 are local variables.
Then I tried to use the same signature: "this(int val1, int val2)" but that didn't work either.
I just wanted to know that why are final and abstract called as modifiers ,what is the essence of calling them as modifiers since there are two types of modifiers access modifiers and non-access modifiers so final and abstract come under the second category ,so why are these called as modifiers?
I am unable to understand the meaning of this sentence "final reference variables must be initialized before the constructor completes.",What is trying to imply?
why using the get method(c.get(c.HOUR_OF_DAY)); gives me the correct hour(currently 19) but directly accesing c.HOUR_OF_DAY returns 11 ? It shows In the documentation that HOUR_OF_DAY is public.
import java.util.*; public class calendar2 { public static void main(String[] args) { new calendar2().timer(); } private void timer() { Calendar c=Calendar.getInstance(); //c.clear(); System.out.println(c.get(c.HOUR_OF_DAY)); System.out.println(c.HOUR_OF_DAY);
How can i take run time value for static final variable...my lecturer said first time assignment is possible for declared final variable but in my case it shows compile time error..I'm placing my program below with error message
class Sample { static final String cname; void print() { System.out.println(cname); } public static void main(String args[]) { cname=args[0]; Sample s=new Sample(); s.print(); } }
Sample.java:11: cannot assign a value to final variable cname. cname=args[0];
For this assignment you will be writing a grade book program. The program will work for one student. It will need to take as input the students name. The user will then be asked to input grades into three categories in this order:
1) homework; 2) quizzes; 3) tests.
The grades in a given category will be averaged to one number that is the average of all grades in that category. The final average will be the weighted average of each category, where homework is worth 25% quizzes are worth 25%, and test are worth 50%. Like this:
Homework Grades: 65, 70, 75, 80, 80 Homework Average: 74 Quiz Grades: 75, 80, 85, 80. Quiz Average: 80 Test Grades: 75, 80, 85, 75 Test Average: 78.75 Final Average = 0.25*HomeworkAvg + 0.25*QuizAvg + 0.50*TestAvg = 77.87
But i only have the average and i dont know how to move past that..
Heres my average code :
import java.util.Scanner; public class Homework3 { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); int gradeCount = 0; int grades = 0; int holder = 0;
Im working on a class to add to my final project but im getting a parsing error for the final bracket, i double checked to make sure im not missing any but im still getting an error
import java.util.Scanner; public class totalprice { public static void main(String[] args) { scanner keyboard = new Scanner(System.in); { DecimalFormat num = new DecimalFormat("#.00"); char meal; int ammount; double cost;
While reading head first java i encountered a problem(Pg. 90 chapter 4 - mixed messages).
Suppose in a class(say A) outside main() a counter variable is declared and initialized to 0.
In main() declared the array of objects of the class A.
Consider a while loop in which we increment the counter as follows:
public class A{ int counter = 0; public static void main(String[] args){ A[] arr = new A[20]; int x = 0; while(x<4){ arr[x] = new A(); //arr[] is array object arr[x].counter += 1; x++; } } };
what is the final value of counter ? will it be the same for all array objects.
class A { final Object b; public A(C c) { try { b = c.someMethodThatMayThrowSomeException(); } catch (SomeException e) { b = null; // This line results in compiler error: "variable b might already have been assigned" } } // take away b=null line and you get "variable b might not have been initialized" on this line }
Why? How could 'b' be assigned if the exception was thrown?