• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Why Cant i Call a method in a class with a constructor?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an example of what i am working on. i cant change anything around just add to it. I hope someone can make some sense out of this for me because i have searched all over trying to figure out why this statement isnt calling the method. the object that i have created goes straight to the constructor in the second class.


Second Class

 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joshua,
Welcome to CodeRanch!

I'm not sure if I am understanding you. Are you asking why the second constructor is called before the doSomething method?


If so, the reason is that Java needs to evaluate the method parameters before calling the method. So it instantiates a new ConstructorClass object (calling that second constructor) and then passes it to the doSomething method.
 
Joshua Kujawa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So my real question is, i guess you could say, is how do i call "try to read this" in a different method such as , given that code. I do understand what your saying thus far, meaning the logic behind it, but i dont know the syntax to call and print "try to read this" after it has been sent to the ConstructorClass. the real confusion for me is the line and i actually think i named the method wrong... the method should be sorry for the confusion.
 
Jeanne Boyarsky
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok. I understand the question now.

This code doesn't compile because ConstructorClass requires a string parameter.


This does compile:

Do you see what it prints out?

This one compiles too. Do you see what it prints out when used in your example?


Now you have the pieces. How do you pass the string you want to print out?

I'm asking a lot of questions to help teach. It's ok if you don't know the answer to any of the questions. Just reply with what you got up to.
 
Joshua Kujawa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i understand how you are passing the string "example" into the constructor then from the constructor to the method and then calling it with but how do i get what is initially passed into the constructor: called out with i think the reason why "example" and "test" worked were because we created a whole new object, wich in fact we had to do because 'obj' is a local object in the main method, and they were strings that were already initialized in the "Constructor" class, am i right? but really i am trying to 1)create an object, 2) call "giveNumbers()" and print "try to read this" in the printLine() method. So really what the question is: why cant i create an object and call the "giveNubers()" method from the ConstructorClass? i dont understand i have done it before in other projects, but i think it has to do with the main method making an object and setting the constructor initially, which is "instantiation" ...right? Keep learning me.
 
Jeanne Boyarsky
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To call the constructor and printNumbers, you'd need to pass in the desired Stirng.

In your example, doSomething() has access to the string you want. So you could call the getter there and store it in the example variable.
 
Marshal
Posts: 76403
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

You are right to give each of those classes a constructor. In each case, what you are telling the compiler includes this:-
“You must only instantiate this class if there is a String passed as an argument.”
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your main program you create a new ConstructorClass("Try to read this") and pass that class to doSomething(). But doSomethoing() ignores what you pass it.
And then in printNumbers() you create a separate ConstructorClass - and as others have said, incorrectly fail to give it a required parameter.
I think what you want is something like this:
 
Joshua Kujawa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah with all the fiddling that I did last night and today I was on the right track with the but what I failed to do was and assigning the cc object in doSomething() method. I knew that you could set an object null, but I didn't know the process of initiating it. I have been hacking away at java for about a month now and I have a lot to learn. Thank you for teaching me something.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic