Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Cannot Reference a non static field from a static context error

User_KIJWJOct 21 2022

/**
* practice
*/
public class Practice {

public int[] myArray = {1,2,3,4,5,6};

public void sumAndProduct(int [] arr){
int sum = 0;
int product = 1;
for(int i = 0; i < myArray.length; i++){
sum += myArray[i];
product *= myArray[i];
}
System.out.println("The sum is: " + sum);
System.out.println("The product is: " + product);
}

public static void main(String[] args) {
Practice demo = new Practice();
demo.sumAndProduct(myArray);

}

}

Comments
Post Details
Added on Oct 21 2022
3 comments
287 views