How do I invoke methods on a Java object created by a different method?












-1















I have a method which creates an object called "OtherItem1". The "getItemName" method is from the class, and returns a String called "itemName". This works fine when I call the "getItemName" method on the "OtherItem1" object within this method. However when I am not able to call this method on the object within my main method. Is there any way to make this object accessible for other methods?



public static void createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();
}

public static void main(String args) {
createOtherItem();
OtherItem1.getItemName();
}









share|improve this question

























  • OtherItem1 is not in scope in your main() method. It was defined inside of createOtherItem() and is only visible there. Try defining your method as follows: public static OtherItem createOtherItem() and then return OtherItem1 at the end of that method.

    – Tom Drake
    Nov 20 '18 at 20:55
















-1















I have a method which creates an object called "OtherItem1". The "getItemName" method is from the class, and returns a String called "itemName". This works fine when I call the "getItemName" method on the "OtherItem1" object within this method. However when I am not able to call this method on the object within my main method. Is there any way to make this object accessible for other methods?



public static void createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();
}

public static void main(String args) {
createOtherItem();
OtherItem1.getItemName();
}









share|improve this question

























  • OtherItem1 is not in scope in your main() method. It was defined inside of createOtherItem() and is only visible there. Try defining your method as follows: public static OtherItem createOtherItem() and then return OtherItem1 at the end of that method.

    – Tom Drake
    Nov 20 '18 at 20:55














-1












-1








-1








I have a method which creates an object called "OtherItem1". The "getItemName" method is from the class, and returns a String called "itemName". This works fine when I call the "getItemName" method on the "OtherItem1" object within this method. However when I am not able to call this method on the object within my main method. Is there any way to make this object accessible for other methods?



public static void createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();
}

public static void main(String args) {
createOtherItem();
OtherItem1.getItemName();
}









share|improve this question
















I have a method which creates an object called "OtherItem1". The "getItemName" method is from the class, and returns a String called "itemName". This works fine when I call the "getItemName" method on the "OtherItem1" object within this method. However when I am not able to call this method on the object within my main method. Is there any way to make this object accessible for other methods?



public static void createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();
}

public static void main(String args) {
createOtherItem();
OtherItem1.getItemName();
}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 20:50









Carcigenicate

18.3k43262




18.3k43262










asked Nov 20 '18 at 20:43









GaneshGanesh

31




31













  • OtherItem1 is not in scope in your main() method. It was defined inside of createOtherItem() and is only visible there. Try defining your method as follows: public static OtherItem createOtherItem() and then return OtherItem1 at the end of that method.

    – Tom Drake
    Nov 20 '18 at 20:55



















  • OtherItem1 is not in scope in your main() method. It was defined inside of createOtherItem() and is only visible there. Try defining your method as follows: public static OtherItem createOtherItem() and then return OtherItem1 at the end of that method.

    – Tom Drake
    Nov 20 '18 at 20:55

















OtherItem1 is not in scope in your main() method. It was defined inside of createOtherItem() and is only visible there. Try defining your method as follows: public static OtherItem createOtherItem() and then return OtherItem1 at the end of that method.

– Tom Drake
Nov 20 '18 at 20:55





OtherItem1 is not in scope in your main() method. It was defined inside of createOtherItem() and is only visible there. Try defining your method as follows: public static OtherItem createOtherItem() and then return OtherItem1 at the end of that method.

– Tom Drake
Nov 20 '18 at 20:55












3 Answers
3






active

oldest

votes


















0














you could declare it as a static variable outside the method.



private static OtherItem OtherItem1;

public static void createOtherItem() {
// existing code here, down to the object creation...

// it's already declared, so you don't need to specify the type again
OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);
OtherItem1.getItemName();
}


But as Carcigenicate suggested, I think it would be better to just return it to the calling method and use it as a local variable there too.



public static OtherItem createOtherItem() {
// Existing code
return OtherItem1;
}
public static void main(String args) {
OtherItem otherItem1 = createOtherItem();
otherItem1.getItemName();
}





share|improve this answer
























  • Thanks! I also like this approach to solving the problem!

    – Ganesh
    Nov 21 '18 at 21:24



















2














OtherItem1 exists only in the scope of method createOtherItem and not outside of it.



You can for example return OtherItem1 from the method and use this object:



public static OtherItem createOtherItem() {
...
return OtherItem1;
}


And then in method main:



OtherItem item = createOtherItem();


And please do not use names beginning with uppercase letters for variable names. This is against generally accepted naming conventions.






share|improve this answer


























  • I like this answer. Note that there are technically other ways (e.g. using a global variable), but this is probably the best. Using a global variable, for example, would not go well with having your methods be static

    – MyStackRunnethOver
    Nov 20 '18 at 20:58











  • Thanks, that makes sense! You are totally right about the naming conventions, my bad!

    – Ganesh
    Nov 21 '18 at 21:21



















1














Just return the object from the method.



Change the signature of the method to:



//            No longer void
public static OtherItem createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();

// Return it here
return OtherItem1;
}

public static void main(String args) {

// Then receive the returned item here
OtherItem returnedItem = createOtherItem();

// And use it here
returnedItem.getItemName();
}


A couple side notes:




  • Your indentation is very weird. 4 spaces of indentation is standard. You seem to be mixing tabs and spaces, and use 6 spaces, or something. It's hard to tell what's going on in the mobile editor, but it was difficult to align properly.


  • Do not use capital names for plain variables. Capital names (like OtherItem1) are reserved for class/interface names. Improper naming conventions makes your code more difficult to read.







share|improve this answer


























  • Thank You, that was very helpful! The indentation is done automatically by Eclipse, so not sure how I would go about changing that. You are completely correct about object names, that was my bad.

    – Ganesh
    Nov 21 '18 at 21:22













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401201%2fhow-do-i-invoke-methods-on-a-java-object-created-by-a-different-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














you could declare it as a static variable outside the method.



private static OtherItem OtherItem1;

public static void createOtherItem() {
// existing code here, down to the object creation...

// it's already declared, so you don't need to specify the type again
OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);
OtherItem1.getItemName();
}


But as Carcigenicate suggested, I think it would be better to just return it to the calling method and use it as a local variable there too.



public static OtherItem createOtherItem() {
// Existing code
return OtherItem1;
}
public static void main(String args) {
OtherItem otherItem1 = createOtherItem();
otherItem1.getItemName();
}





share|improve this answer
























  • Thanks! I also like this approach to solving the problem!

    – Ganesh
    Nov 21 '18 at 21:24
















0














you could declare it as a static variable outside the method.



private static OtherItem OtherItem1;

public static void createOtherItem() {
// existing code here, down to the object creation...

// it's already declared, so you don't need to specify the type again
OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);
OtherItem1.getItemName();
}


But as Carcigenicate suggested, I think it would be better to just return it to the calling method and use it as a local variable there too.



public static OtherItem createOtherItem() {
// Existing code
return OtherItem1;
}
public static void main(String args) {
OtherItem otherItem1 = createOtherItem();
otherItem1.getItemName();
}





share|improve this answer
























  • Thanks! I also like this approach to solving the problem!

    – Ganesh
    Nov 21 '18 at 21:24














0












0








0







you could declare it as a static variable outside the method.



private static OtherItem OtherItem1;

public static void createOtherItem() {
// existing code here, down to the object creation...

// it's already declared, so you don't need to specify the type again
OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);
OtherItem1.getItemName();
}


But as Carcigenicate suggested, I think it would be better to just return it to the calling method and use it as a local variable there too.



public static OtherItem createOtherItem() {
// Existing code
return OtherItem1;
}
public static void main(String args) {
OtherItem otherItem1 = createOtherItem();
otherItem1.getItemName();
}





share|improve this answer













you could declare it as a static variable outside the method.



private static OtherItem OtherItem1;

public static void createOtherItem() {
// existing code here, down to the object creation...

// it's already declared, so you don't need to specify the type again
OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);
OtherItem1.getItemName();
}


But as Carcigenicate suggested, I think it would be better to just return it to the calling method and use it as a local variable there too.



public static OtherItem createOtherItem() {
// Existing code
return OtherItem1;
}
public static void main(String args) {
OtherItem otherItem1 = createOtherItem();
otherItem1.getItemName();
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 20:58









BlairBlair

665




665













  • Thanks! I also like this approach to solving the problem!

    – Ganesh
    Nov 21 '18 at 21:24



















  • Thanks! I also like this approach to solving the problem!

    – Ganesh
    Nov 21 '18 at 21:24

















Thanks! I also like this approach to solving the problem!

– Ganesh
Nov 21 '18 at 21:24





Thanks! I also like this approach to solving the problem!

– Ganesh
Nov 21 '18 at 21:24













2














OtherItem1 exists only in the scope of method createOtherItem and not outside of it.



You can for example return OtherItem1 from the method and use this object:



public static OtherItem createOtherItem() {
...
return OtherItem1;
}


And then in method main:



OtherItem item = createOtherItem();


And please do not use names beginning with uppercase letters for variable names. This is against generally accepted naming conventions.






share|improve this answer


























  • I like this answer. Note that there are technically other ways (e.g. using a global variable), but this is probably the best. Using a global variable, for example, would not go well with having your methods be static

    – MyStackRunnethOver
    Nov 20 '18 at 20:58











  • Thanks, that makes sense! You are totally right about the naming conventions, my bad!

    – Ganesh
    Nov 21 '18 at 21:21
















2














OtherItem1 exists only in the scope of method createOtherItem and not outside of it.



You can for example return OtherItem1 from the method and use this object:



public static OtherItem createOtherItem() {
...
return OtherItem1;
}


And then in method main:



OtherItem item = createOtherItem();


And please do not use names beginning with uppercase letters for variable names. This is against generally accepted naming conventions.






share|improve this answer


























  • I like this answer. Note that there are technically other ways (e.g. using a global variable), but this is probably the best. Using a global variable, for example, would not go well with having your methods be static

    – MyStackRunnethOver
    Nov 20 '18 at 20:58











  • Thanks, that makes sense! You are totally right about the naming conventions, my bad!

    – Ganesh
    Nov 21 '18 at 21:21














2












2








2







OtherItem1 exists only in the scope of method createOtherItem and not outside of it.



You can for example return OtherItem1 from the method and use this object:



public static OtherItem createOtherItem() {
...
return OtherItem1;
}


And then in method main:



OtherItem item = createOtherItem();


And please do not use names beginning with uppercase letters for variable names. This is against generally accepted naming conventions.






share|improve this answer















OtherItem1 exists only in the scope of method createOtherItem and not outside of it.



You can for example return OtherItem1 from the method and use this object:



public static OtherItem createOtherItem() {
...
return OtherItem1;
}


And then in method main:



OtherItem item = createOtherItem();


And please do not use names beginning with uppercase letters for variable names. This is against generally accepted naming conventions.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 11:03

























answered Nov 20 '18 at 20:56









DonatDonat

848128




848128













  • I like this answer. Note that there are technically other ways (e.g. using a global variable), but this is probably the best. Using a global variable, for example, would not go well with having your methods be static

    – MyStackRunnethOver
    Nov 20 '18 at 20:58











  • Thanks, that makes sense! You are totally right about the naming conventions, my bad!

    – Ganesh
    Nov 21 '18 at 21:21



















  • I like this answer. Note that there are technically other ways (e.g. using a global variable), but this is probably the best. Using a global variable, for example, would not go well with having your methods be static

    – MyStackRunnethOver
    Nov 20 '18 at 20:58











  • Thanks, that makes sense! You are totally right about the naming conventions, my bad!

    – Ganesh
    Nov 21 '18 at 21:21

















I like this answer. Note that there are technically other ways (e.g. using a global variable), but this is probably the best. Using a global variable, for example, would not go well with having your methods be static

– MyStackRunnethOver
Nov 20 '18 at 20:58





I like this answer. Note that there are technically other ways (e.g. using a global variable), but this is probably the best. Using a global variable, for example, would not go well with having your methods be static

– MyStackRunnethOver
Nov 20 '18 at 20:58













Thanks, that makes sense! You are totally right about the naming conventions, my bad!

– Ganesh
Nov 21 '18 at 21:21





Thanks, that makes sense! You are totally right about the naming conventions, my bad!

– Ganesh
Nov 21 '18 at 21:21











1














Just return the object from the method.



Change the signature of the method to:



//            No longer void
public static OtherItem createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();

// Return it here
return OtherItem1;
}

public static void main(String args) {

// Then receive the returned item here
OtherItem returnedItem = createOtherItem();

// And use it here
returnedItem.getItemName();
}


A couple side notes:




  • Your indentation is very weird. 4 spaces of indentation is standard. You seem to be mixing tabs and spaces, and use 6 spaces, or something. It's hard to tell what's going on in the mobile editor, but it was difficult to align properly.


  • Do not use capital names for plain variables. Capital names (like OtherItem1) are reserved for class/interface names. Improper naming conventions makes your code more difficult to read.







share|improve this answer


























  • Thank You, that was very helpful! The indentation is done automatically by Eclipse, so not sure how I would go about changing that. You are completely correct about object names, that was my bad.

    – Ganesh
    Nov 21 '18 at 21:22


















1














Just return the object from the method.



Change the signature of the method to:



//            No longer void
public static OtherItem createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();

// Return it here
return OtherItem1;
}

public static void main(String args) {

// Then receive the returned item here
OtherItem returnedItem = createOtherItem();

// And use it here
returnedItem.getItemName();
}


A couple side notes:




  • Your indentation is very weird. 4 spaces of indentation is standard. You seem to be mixing tabs and spaces, and use 6 spaces, or something. It's hard to tell what's going on in the mobile editor, but it was difficult to align properly.


  • Do not use capital names for plain variables. Capital names (like OtherItem1) are reserved for class/interface names. Improper naming conventions makes your code more difficult to read.







share|improve this answer


























  • Thank You, that was very helpful! The indentation is done automatically by Eclipse, so not sure how I would go about changing that. You are completely correct about object names, that was my bad.

    – Ganesh
    Nov 21 '18 at 21:22
















1












1








1







Just return the object from the method.



Change the signature of the method to:



//            No longer void
public static OtherItem createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();

// Return it here
return OtherItem1;
}

public static void main(String args) {

// Then receive the returned item here
OtherItem returnedItem = createOtherItem();

// And use it here
returnedItem.getItemName();
}


A couple side notes:




  • Your indentation is very weird. 4 spaces of indentation is standard. You seem to be mixing tabs and spaces, and use 6 spaces, or something. It's hard to tell what's going on in the mobile editor, but it was difficult to align properly.


  • Do not use capital names for plain variables. Capital names (like OtherItem1) are reserved for class/interface names. Improper naming conventions makes your code more difficult to read.







share|improve this answer















Just return the object from the method.



Change the signature of the method to:



//            No longer void
public static OtherItem createOtherItem() {
System.out.print("Item Name : ");
itemName = input.next();
System.out.print("Price : ");
price = input.nextDouble();
System.out.print("Id : ");
id = input.nextInt();
System.out.print("Stock: ");
stock = input.nextInt();
System.out.print("Department : ");
department = input.nextInt();
System.out.print("Details : ");
details = input.next();

OtherItem OtherItem1 = new OtherItem(itemName, price, id, stock, department, details);

OtherItem1.getItemName();

// Return it here
return OtherItem1;
}

public static void main(String args) {

// Then receive the returned item here
OtherItem returnedItem = createOtherItem();

// And use it here
returnedItem.getItemName();
}


A couple side notes:




  • Your indentation is very weird. 4 spaces of indentation is standard. You seem to be mixing tabs and spaces, and use 6 spaces, or something. It's hard to tell what's going on in the mobile editor, but it was difficult to align properly.


  • Do not use capital names for plain variables. Capital names (like OtherItem1) are reserved for class/interface names. Improper naming conventions makes your code more difficult to read.








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 21:34

























answered Nov 20 '18 at 20:57









CarcigenicateCarcigenicate

18.3k43262




18.3k43262













  • Thank You, that was very helpful! The indentation is done automatically by Eclipse, so not sure how I would go about changing that. You are completely correct about object names, that was my bad.

    – Ganesh
    Nov 21 '18 at 21:22





















  • Thank You, that was very helpful! The indentation is done automatically by Eclipse, so not sure how I would go about changing that. You are completely correct about object names, that was my bad.

    – Ganesh
    Nov 21 '18 at 21:22



















Thank You, that was very helpful! The indentation is done automatically by Eclipse, so not sure how I would go about changing that. You are completely correct about object names, that was my bad.

– Ganesh
Nov 21 '18 at 21:22







Thank You, that was very helpful! The indentation is done automatically by Eclipse, so not sure how I would go about changing that. You are completely correct about object names, that was my bad.

– Ganesh
Nov 21 '18 at 21:22




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401201%2fhow-do-i-invoke-methods-on-a-java-object-created-by-a-different-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)