• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling methods and variables from another class?  RSS feed

 
Glenn Mause
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I have a question.
I know it's total beginner "Greenhorn" Stuff but I need to know how I call a method or string from another class.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. Depending on the package your code is in, you may need to import the package first.

Here's an example of what I mean:



This works because class A and class B are in the same package. But what if they're not? Then you use an import statement. If class B was in its own package, B, you would do it like this:


One more thing to keep in note is the word "public" here. Public means accessible by anyone. If the doStuff method in class A was marked as private, class B would not be able to access it.
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:

I disagree with line 3.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:I disagree with line 3.


Can you elaborate? I didn't understand how to explain it better.
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That statement on line 3 would cause you a compilation error.
I am pretty sure you know why. Look carefully.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:That statement on line 3 would cause you a compilation error.
I am pretty sure you know why. Look carefully.


I see! Sorry, it's been a long day. ^^;

An import statement should look like the following:


You could also do import A.*;, which imports all the classes from that package, but you typically don't want to do that unless you're absolutely sure you need all those classes in a given package.
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo. I knew you know that
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:

I must comment on your comment after the import statement, because it's incorrect! An import statement NEVER brings a type (class, interface,...) into scope. With an import statement you can use the simple name of a type (class, interface,...) instead of the fully qualified name. And that's really the only purpose of an import statement. Using import statements your code will be less cluttered and become more readible.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they are in same .java file
Then try out ways to send the reference of object of class whose method(instance method) you want to use, to the class where you want to use
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:If they are in same .java file
Then try out ways to send the reference of object of class whose method(instance method) you want to use, to the class where you want to use

I have no clue what you are talking about here. Could you elaborate a bit more or explain it once again? Thanks!
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Tim Harris wrote:

I must comment on your comment after the import statement, because it's incorrect! An import statement NEVER brings a type (class, interface,...) into scope. With an import statement you can use the simple name of a type (class, interface,...) instead of the fully qualified name. And that's really the only purpose of an import statement. Using import statements your code will be less cluttered and become more readible.


I knew this, but again, I was at the end of a long day and my brain was somewhat fried!

You're absolutely right - importing packages allows for simpler, more readable code, and does not affect scope at all.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:I knew this, but again, I was at the end of a long day and my brain was somewhat fried!

No problem! I'm glad you already know this. So that comment was not really helpful for you, but it probably will for other ranchers who just started learning Java.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not clearing things properly
I assume op is trying to ask
"How to call methods of a class(X)
From another class(Y)"

So I suggested him to check out by how many ways he can pass the reference of an object of type(X) to class (Y)

Like in the main method of (X) we create object of type (X)
1-and pass it to the constructor of (Y)
X x=new X();
Y y=new Y(x);

2-pass it to setter method of Y
y.set(x);

3-pass it to ordinary method of Y
y.xyz(x);


Now in class Y if it has an instance variable as
X x;

Then this can receive instance of X by:
1-constructor (but this will be applicable to every instance of Y)
public Y(X x)
{
this.x=x;
}


2-or setter method(it can be for selected instance of Y,but for indeterminate time)
void set(X x)
{
this.x=x;
}

Now if we want it for definite time
void xyz(X x)
{
x.methodofX();
}

Hope this helps
 
Glenn Mause
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys thank you so many answers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!