Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need basic advice on getting int variable  RSS feed

 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to get int myClass to println 3 times.
Here is the code. Each code is a seperate java file because this is how I wanted to do it.

________________________

public class MyClassC {
int myClass = 100;
}
________________________

public class MyClassB {

public MyClassB() {
MyClassC myNewClass = new MyClassC();
int x = myNewClass.myClass;
System.out.println (x);

}
}
________________________

public class MyClassA {

public MyClassA(){
MyclassB printOut = new MyClassB();
}

public static void main(String[] args) {
MyClassA();
MyClassA();
MyClassA();
}

}
________________________
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides the fact that your code won't compile, what problems are you having?
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I know it wont compile because I compiled it. LoL.

Anyways Im trying to get int myClass in MyClassC to print in MyClassB (which I got to do) but now I want to print it three times in MyClassA. I realize the code itself is probably all messed up but im a newbie so forgive me.

I've spent hours rewriting and researching to get the code to work. But I'm getting frustrated so any help would be appriciated. Thanks.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, I thought you had some other issue and just messily copied the code. I would suggest starting with The Java Tutorial to learn the basic syntax and concepts. The two things that jump out at me are your misspelling of "MyclassB" (it's an upper case C, Java is case sensitive) and that you're trying to create a new MyClassA without using the keyword. In fact, if you fix those two things I'm not noticing anything else wrong with it.
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you may have to use package and import the class without which you cannot use the class members.
You may have to do the following changes

package pkgClassC;
public class MyClassC {
public int myClass = 100;
}
-----> Put the class file in /YourJavaDir/pkgClassC/MyClass.class

Similarly do it for ClassB.

Ken, Please correct me if I am wrong.

-Kashyap
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they're all in the default package they should be okay as is.
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I fixed the case in C, I feel like an idiot, told you I was a newbie.
But now I'm geting this error.

MyClassA.java:8: cannot resolve symbol
symbol : method MyClassA ()
location: class MyClassA
MyClassA();
^
1 error


I also tried to do something similar here to try and figure it out but I get the same message.

class HelloWorld {

public HelloWorld() {
System.out.println ("Hello World");
}

public static void main (String[] args) {
HelloWorld();
}
}
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, you're trying to create objects without using the keyword new. That is not valid syntax. You need to do this:



To create a new object.
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh!

Thanks Ken, that worked great!

Now its time to code a MMORPG. LoL
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java? Good luck.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!