This question already has answers here:
Java instance variable declare and Initialize in two statements
(5 answers)
Closed 6 years ago.
This is a part of code i was working on .. but the compiler showed an error on line 1. (Syntax error on token";", , expected). Why is that error coming??
public class variable
{
int[] nums;
nums= new int[7];
}
You have to initialize the Array in same line as the declaration
public class variable
{
int[] nums = new int[7];
}
or you have to initialize it in a method or constructor:
public class variable
{
int[] nums;
public variable(){
nums= new int[7];
}
}
Hint: read about Java naming conventions. Class names should start with uppercase character.
You should use assignment inside a method or a constructor. Or you can instantiate it class level but you have to initialize it same line with declaration.
Eg: Class level instantiation.
public class Variable {
int[] nums = new int[7];
}
Use inside a method.
public class Variable {
int[] nums;
public void method(){
nums = new int[7];
}
}
Related
This question already has an answer here:
What does "error: '.class' expected" mean and how do I fix it
(1 answer)
Closed 3 years ago.
I'd like to know why I got these errors and how to fix it:
Main.java:12: error: '.class' expected
populateArray(int[] aRand);
^
Main.java:12: error: ';' expected
populateArray(int[] aRand);
The instructions I was given are in the comments and I just followed what I believe what the instructions meant. I thought maybe I should do populateArray(); in the main method, but it wouldn't match the signature of public void populateArray(int[] array){}.
import java.util.Random;
public class Main {
public static void main(String args[])
{
/*
(a) Create an array of integers called aRand of capacity 20 and initialize its entries to 0.
*/
int aRand[] = new int[20];
populateArray(int[] aRand);
}
/*
(b) Populate the array in a method called populateArray(int[] array) where array is the one you want to populate.
This method returns void.
*/
public void populateArray(int[] array){
Random rand = new Random();
for (int i=0; i<array.length; i++){
array[i] = rand.nextInt();
System.out.println(array[i]);
}
}
}
Just a couple small things!
First, you already declared populateArray as a method that takes an int[], so you do not need to do so when you actually call the method.
Second, you cannot call a non-static method from a static context. Since your main method is static, you cannot call the non-static method populateArray within it. For your purposes, it should be fine to just declare populateArray as a static method.
import java.util.Random;
public class Main {
public static void main(String args[])
{
/*
(a) Create an array of integers called aRand of capacity 20 and initialize its entries to 0.
*/
int aRand[] = new int[20];
populateArray(aRand); // We already know aRand is an int[]
}
/*
(b) Populate the array in a method called populateArray(int[] array) where array is the one you want to populate.
This method returns void.
*/
public static void populateArray(int[] array){
Random rand = new Random();
for (int i=0; i<array.length; i++){
array[i] = rand.nextInt();
System.out.println(array[i]);
}
}
}
For reference, here as a helpful StackOverflow answer that discusses when you should use static vs. instance methods: Java: when to use static methods.
This question already has answers here:
Java: When is a static initialization block useful?
(13 answers)
Closed 5 years ago.
Why is this declaration wrong? This declaration leads to identifier expected error
class Abc{
static ArrayList<Integer> p;
p = new ArrayList<Integer>(); // identifier expected error
}
You have a freestanding assignment statement in your class body. You can't have step-by-step code there, it has to be within something (an initializer block, a method, a constructor, ...). In your specific case, you can:
Put that on the declaration as an initializer
static ArrayList<Integer> p = new ArrayList<>();
Wrap it in a static initialization block
static {
p = new ArrayList<Integer>();
}
More in the tutorial on initializing fields.
This is the right way to do it :
import java.util.ArrayList;
public class Abc {
static ArrayList<Integer> p;
static {
p = new ArrayList<Integer>(); // works
}
}
This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
Closed 7 years ago.
I am new to Java and started doing Arrays, however I am getting Compilation error with the below code. Pls help
public class TestingArrays {
int[] ank = new int[]{1,2,3,4,5};
int[] ans = new int[5];
ans[0] = 2;
}
If I comment the line //ans[0] = 2; then the error is gone, please explain
This is a class definition. You are allowed to declare and initialize members and methods. You are not allowed to write code as you would in a function body. Your code would work if you modify like this (Constructor):
public class TestingArrays {
int[] ank = new int[]{1,2,3,4,5};
int[] ans = new int[5];
TestingArrays() {
ans[0] = 2;
}
}
Or even like this (Initialization block) :
public class TestingArrays {
int[] ank = new int[]{1,2,3,4,5};
int[] ans = new int[5];
{
ans[0] = 2;
}
}
Do the initialization in a constructor or instance initializer.
TestingArrays() {
ans[0] = s;
}
You cannot have statements within the class body such as setting a value to a field.
This question already has answers here:
Passing directly an array initializer to a method parameter doesn't work
(3 answers)
Closed 8 years ago.
I am writing test code to explore properties of an Array.
Why does this work
public static void main(String[] args){
int[] testing={1,2,3};
for(int i = 0;i<testing.length;i++){
System.out.println(testing[i]);
}
}
and why doesn't this work?
public static void main(String[] args){
int[] testing= new int[3];
testing = {1,2,3};
for(int i = 0;i<testing.length;i++){
System.out.println(testing[i]);
}
}
What is it about Array that prevents this from being valid?
Because the Java Language Specification says so
An array initializer may be specified in a declaration (§8.3, §9.3,
§14.4), or as part of an array creation expression (§15.10), to create
an array and provide some initial values.
So you can either use it as you are already doing
int[] testing = {1,2,3};
or as part of an array creation expression
testing = new int[]{1,2,3};
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 3 years ago.
This is my first post here, so please be friendly! :)
Let's say I have this code:
public static void reassign (int[] nums) {
int[] A = {10,11,22};
A = nums;
}
public static void main(String[] args) {
int[] nums = {0,2};
reassign(nums);
System.out.println(nums[1]);
}
Why is my answer 2, and not 11? Does it have something to do with the relative sizes of the arrays?
When you do this,
public static void reassign (int[] nums) {
int[] A = {10,11,22};
A = nums;
}
you make A as a refference of nums, and the nums you are refering to is the one from parameter, not the one from main method. its two different variable
This is how you suppose to do it:
static int[] nums = {0,2}; //initial value of nums
public static void reassign (int[] arr) {
nums=arr;
}
public static void main(String[] args) {
int[] A = {10,11,22};
System.out.println("before reassign:"+nums[1]);
reassign(A);
System.out.println("after reassign:"+nums[1]);
}
Output:
before reassign:2
after reassign:11
Why is my answer 2, and not 11?
For one, you wanted to write
public static void reassign (int[] nums) {
int[] A = {10,11,22};
nums = A;
}
But than won't help you either: you cannot assign to one method's local variable from another method. The value of the variable (a reference to the array) is passed to reassign, not the address of the variable itself.
In reassign you merely create a new array and then assign its reference to the local variable nums (in my corrected code).
Does it have something to do with the relative sizes of the arrays?
No, the reason behind it is fully general and applies to all of Java.