I'm new to Java and can't seem to understand why this code doesn't work. Any input would be appreciated. Thx.

Public class MyClass
    int a;
    float b = 1.5;
    
    public void static main(String args) {
        MyClass mc = MyClass();
        mc.a = 1.5;
        system.out.println(b);
    }

Java is not a markup language where upper and lower case doesn't matter. Same goes for brackets and opening and closing. Lastly, there is a specific way to write the main method.

So based on what I said, what your compiler tells you about the above code?

Going by the compiler feedback, I made some adjustments but still cannot figure out what's wrong with the code. I'm VERY new to Java and seem to be stuck not understanding fully wha tthe compiler is telling me, which is why I posted it here.

Here's what I have so far:

public class MyClass
{
    int a;
    double b = 1.5;
    
    public static void main(String args) {
        MyClass mc = MyClass();
        int a = 1.5;
        System.out.println(b);
    }
}

Any input or help would be appreciated. Thx.

Well lets see what compiler says about this code

C:\Projects - Home\Daniweb\MyClass.java:7: cannot find symbol
symbol  : method MyClass()
location: class MyClass
        MyClass mc = MyClass();
                     ^
C:\Projects - Home\Daniweb\MyClass.java:8: possible loss of precision
found   : double
required: int
        int a = 1.5;
                ^
C:\Projects - Home\Daniweb\MyClass.java:9: non-static variable b cannot be referenced from a static context
        System.out.println(b);
                           ^

First error is telling you that it cannot find symbol method MyClass(). As you did not tell compiler that you want to cast new instance of your class its automatically assume that you want to call method of same name as is class, but that method does not exists. So what needs to be done?

Second error possible loss of precision. Your variable "a" is of type int, however for once you are redeclaring its type which you shouldn't. Secondly on integer you are trying to force double. So what did you wanted to do here?

Third error non-static variable b cannot be referenced from a static context. As error states you"b" have to be declare static if you want to use it from here.

Here's my updates. It appears to have compiled OK.

public class MyClass
{
    public static void main(String args) {
        MyClass mc = MyClass();
        double a = 1.5;
        double b = 1.5;
        System.out.println(b);
    }

	private static MyClass MyClass() {
		return null;
	}
}
public static void main(String[] args)

is different than what you have.

That did it! Not sure how I missed that, but that's why it's good to have a fresh set of eyes look at it. Thanks much to both of you for your assitance with this!!!

public class MyClass
{
    public static void main(String[] args) {
        MyClass mc = MyClass();
        double a = 1.5;
        double b = 1.5;
        System.out.println(b);
    }

	private static MyClass MyClass() {
		return null;
	}
}
private static MyClass MyClass() {
		return null;
	}

What are you trying to achieve with this declaration?

Honestly? Not really sure. I am using Eclipse to validate some of my code and that piece of code was what Eclipse recommended to make everything work. If I take that section out, the error I get is:

C:\java>javac MyClass.java
MyClass.java:4: cannot find symbol
symbol : method MyClass()
location: class MyClass
MyClass mc = MyClass();
^
1 error

I'm not sure how to fix that error. If you have a better options that works, I would sure be appreciative since I'm just a couple of days new to this language and need to learn it for a class I'm taking.

Delete that method altogether. Your mistake is here:

MyClass mc = MyClass();

Should be

MyClass mc = new MyClass();

That worked! Thanks BestJewSinceJC!!! You're saving me a lot of long time headaches here. Much appreciated.

That worked! Thanks BestJewSinceJC!!! You're saving me a lot of long time headaches here. Much appreciated.

You wouldn't have such problems if you pay attention what you been told. Post #4 I explained already what you doing wrong and you repeated same mistake. Shame...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.