Jump to content
Trey

The Basics of Java

Recommended Posts

Whats the error in this? I am trying to initialize D to find out the value.

[CODE]

public class Main {
int a=1;
int b=1;
int c=1;
int d=a + b + c;

public static void main(String[] args) {
System.out.println(d);
}
}
[/CODE]


Error:
[QUOTE]
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static field d

at main.main(main.java:8)
[/QUOTE]


I am using eclipse.

Share this post


Link to post
Share on other sites
[quote name='Mish']Whats the error in this? I am trying to initialize D to find out the value.

[CODE]

public class Main {
int a=1;
int b=1;
int c=1;
int d=a + b + c;

public static void main(String[] args) {
System.out.println(d);
}
}
[/CODE]
Error:



I am using eclipse.[/QUOTE]

You have to make the field d static, and then all the other ones static too.

Share this post


Link to post
Share on other sites
[quote name='Steve']You have to make the field d static, and then all the other ones static too.[/QUOTE]

Thanks it runs perfectly now.

Share this post


Link to post
Share on other sites
[quote name='Mish']Whats the error in this? I am trying to initialize D to find out the value.

[CODE]

public class Main {
int a=1;
int b=1;
int c=1;
int d=a + b + c;

public static void main(String[] args) {
System.out.println(d);
}
}
[/CODE]


Error:



I am using eclipse.[/QUOTE]

Yeah, you can't reference instance fields and methods from a static context. Instance fields and instance methods exist on an object basis, while static fields and methods exist on a class basis. All objects of a class share the same static field, while all of those objects may have a different value for an instance field. Thus, it's impossible to use an instance field or method from a static one.

Share this post


Link to post
Share on other sites
[quote name='Trey']Yeah, you can't reference instance fields and methods from a static context. Instance fields and instance methods exist on an object basis, while static fields and methods exist on a class basis. All objects of a class share the same static field, while all of those objects may have a different value for an instance field. Thus, it's impossible to use an instance field or method from a static one.[/QUOTE]

Okay, thank-you.

Share this post


Link to post
Share on other sites
[QUOTE]Big-O is a notation used to measure efficiency of an algorithm to complete a specific task.[/QUOTE]

i swear you measure the efficiency by analysis, not by the notation. :P

Share this post


Link to post
Share on other sites
[quote name='_s1gma']i swear you measure the efficiency by analysis, not by the notation. :P[/QUOTE]

Not necessarily. Again, speed and efficiency are not the same. You can measure memory usage or the speed of an algorithm, but big-o notates the efficiency in the sense of how much "work" an algorithm must do to complete a task. The most efficient algorithms aren't always the fastest or the most conservative in memory.

Share this post


Link to post
Share on other sites
[quote name='Trey']Not necessarily. Again, speed and efficiency are not the same. You can measure memory usage or the speed of an algorithm, but big-o notates the efficiency in the sense of how much "work" an algorithm must do to complete a task. The most efficient algorithms aren't always the fastest or the most conservative in memory.[/QUOTE]

my point was that the notation doesn't measure, it notates, as you said.

Share this post


Link to post
Share on other sites
[quote name='_s1gma']my point was that the notation doesn't measure, it notates, as you said.[/QUOTE]

Well if you want to be strict about wording, analysis doesn't technically measure anything either.

Share this post


Link to post
Share on other sites
[quote name='Trey']Well if you want to be strict about wording, analysis doesn't technically measure anything either.[/QUOTE]

true. hehe, apologies for being pendatic.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×