Java custom method not working [duplicate] - java

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 7 years ago.
I'm a beginner and I'm starting to learn programming by doing some
exercises...
Why this simple java code gives me an error?
class HelloWorldEdited {
public int a = 5;
public int b = 2;
public static int sum() {
return a + b;
}
public static void main(String[] args) {
HelloWorldEdited obj = new HelloWorldEdited();
System.out.println(obj.sum());
}
}

I think it's because you are accessing "non static" properties (a, b) from a static method (sum), this operation is forbidden.
Try to change
public static int sum()
to
public int sum()
To understand the "static" modifier I suggest you to read:
official tutorial

The method sum() is static. In this method you can't access variables "non static".

Related

Static Variables in Java [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
What is the difference between referencing a field by class and calling a field by object?
(5 answers)
Closed 5 years ago.
I know that static variables are part of class and not part of the Object . How can the following lines of code work without any problem
class M
{
static int i=0;
void Inc()
{
System.out.println("Global "+M.i);
System.out.println("Local "+this.i);
}
}
public class StaticTest
{
public static void main(String args[])
{
M m1=new M();
m1.i=99; //How can the m1 object access i variable of the class
m1.Inc();
}
}
The output i get is
Global 99
Local 99
How can the m1 object access i variable of the class?
It is the very same i variable in both cases.
unfortunately, java allows you to access static fields using non-static syntax.
That is all there is to this, nothing else behind this.
Yes, it is allowed for non-static members to access and update static members.
See this for more information here

Storing reference to object [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I would like to store reference to variable in some class, and make operations on it inside this class. Operations should modify original variable.
In particular following code should print 1 instead of 0.
class Test {
private Long metric;
public Test(Long m) {
this.metric = m;
++this.metric;
}
}
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Long metric = 0L;
Test test = new Test(metric);
System.out.println(metric);
}
}
How to achieve this behaviour?
You can replace Long with AtomicLong which is mutable. You'll lose autoboxing features though.
The problem in your code is that Integer is an immutable class.
Every time that you change the value you are really building a new instance of Integer.
Doing the same with mutable objects will work.
For example
class Test {
private StringBuilder metric;
public Test(StringBuilder m) {
this.metric = m;
this.metric.append(" Xter");
}
}
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
StringBuilder b = new StringBuilder("Hello ");
Test test = new Test(metric);
System.out.println(b.toString());
// Will print Hello Xter
}
}
As already metioned, the primitive wrapper classes are inmutable.
Since your metric is private in Test, and you want to use its value in the calling method main, you should use the java bean guidelines and use a getter for it:
public Long getMetric(){return this.metric;}
And on main:
metric=test.getMetric();
System.out.println(metric);

non-static method cannot be referenced confusion [duplicate]

This question already has answers here:
Calling Non-Static Method In Static Method In Java [duplicate]
(14 answers)
Closed 6 years ago.
Receiving a non-static method cannot be referenced from static context. In this example I deleted all of the extra "stuff." All of the other examples I found had a lot of distractors that confused me.
This is for studying for a final and is NOT part of an assignment.
I do not understand why there is an issue here - troubles understanding static/non-static issue altogether.
In this case all I expect is for 5207 to be the output.
package testcase;
public class Testcase {
int number = 5207;
public static void main(String[] args) {
//int number = 5207;
int div;
div = divisor(number);
System.out.println(div);
}
private int divisor(int num){
return number;
}
Try to become a static method like this:
private static int divisor(int num){
return number;
}
Or instance the object of class Testcase :
Testcase tsc = new Testcase();
div = tsc.divisor(number);

scope of variable inside main in Java? [duplicate]

This question already has answers here:
Why does Java not have block-scoped variable declarations?
(6 answers)
Closed 9 years ago.
Am I correct that a block inside main() doesn't have separate scope?
For example, if I do the following, I will get compile error:
static int i = 1;
public static void main(String[] args) {
int i = 2;
{
int i = 3;
}
}
Why is this?
When I try to compile this, I get this message:
error: variable i is already defined in method main(String[])
This means that the static int i = 1; does not cause the error. The scope of the first i in main is for the whole main, so when you try to make another one, in the code block, you get an error.
Note that if you declared the i in a block:
public static void main(String[] args){
{
int i = 2;
}
{
int i = 3;
}
}
You don't get a compile error (see here).
The static int can be accessed in one of two ways: this.i (not recommended) or ClassName.i (recommended for accessing of a static variable)
Java does not allow obfuscation of stack (ie. local variables). You can obfuscate an instance or class variable because there are other ways to access them (ie. this.i for instance, or MainClass.i for static).

how to use non-static function inside of static function [duplicate]

This question already has an answer here:
An object reference is required for the non-static field, method, or property
(1 answer)
Closed 9 years ago.
Here is the my class..
public class Oop {
int count = 0;
public static void main(String args[])
{
this.count(15, 30);
System.out.print(this.count);
}
public void count(int start, int end)
{
for(;start<end; start++)
{
this.count = this.count + start;
}
}
}
I can't call count function inside of main function. Reason is static and non-static functions. I'm really new for Java. How can i use count inside of main? What i need to learn?
You need to instantiate Oop and then call the method with it, like this:
Oop oop = new Oop();
oop.count(1,1);
For further information check this out: Difference between Static methods and Instance methods
You should make the count function and the count variable static too.
That's the least of your worries - once you've called the method, you won't be able to access the result.
You must create an instance of your class, and use that instance to call your method and get the result:
public static void main(String args[]) {
Oop oop = new Oop();
oop.count(15, 30);
System.out.print(oop.count);
}

Categories