+ 2
As we know "function " is call inside the function only????
If i'm right so how we can this.. 1. create a object at class level 2.how can we call function at class level.. can anybody explain me... i first go through the google first.. but not understand https://code.sololearn.com/c65Guq2attLc/?ref=app
14 Answers
+ 2
Technically, all of the objects you're creating are being created (instantiated) in the program's class (where the main exists). Once you've created an object, you can call any of its methods (functions) from the variable that's holding reference to the object. Get what I mean?
The other day I was helping someone understand classes/objects based upon what they were doing, and I helped them put the code together that they wanted. It's good example for the simple purpose of seeing classes/objects/functions in action. It may be of help:
https://code.sololearn.com/cvTpdrjVW2B6/#java
^Hopefully that'll help out some with understanding and seeing it all work. After you look at it and run it to see it in action, if you have any questions or want me to further elaborate on anything, just let me know bro.
+ 1
can you see my code too pls
+ 1
Sorry, I didn't see the link the first time.
The reason why you can't create an object right there, on that class level, is because you're telling it to create an object from the class you're calling that from. So it goes to create the object, it sees to create an object, so then it goes to create that object and sees that it should create that again, so it goes to create that object and see that it should create that object once again, so it goes to create that objec...... As you can see, you get stuck in an infinite loop when you do that and shuts your program down because it can't do anything else from that point.
So from inside of a class, don't have it create an object based upon that same class, otherwise you'll get stuck. There are ways around this, but it should make you question why you're doing it that way to begin with, as it's really sloppy way of going about it and is prone to error.
Good practice is to keep all of your other classes separate and then call those other classes from the main function/loop/class. This will prevent conflicts or circular references. As well, this helps keep your code clean and modular. When it can be helped, better to not jumbled up your main function with a bunch of junk.
+ 1
By the way, "Exception in thread "main" java.lang.StackOverflowError" is part of your error that you get when you try to create an object, based upon the same class that's calling it, inside of itself. It's overflowing your stack because it's creating an endless stream of objects due to the infinite loop that gets created.
+ 1
i have got no error bro..
pls check again
+ 1
I know. You commented out where you were trying to create the object on the class level. If you uncomment it, it'll give you a stack overflow error message. I was just explaining to you why you can't do that on the class level since it's based upon the same class it's being called from.
+ 1
and what about function create at class level
+ 1
If you look at your code, you did call a function from the class level.
Your Code:
int x=AssignX();
^That is within the class scope. This is calling the function 'AssignX()' and then assigning the returned value to your variable 'x'. In this case, the variable 'x' is part of the class scope, and the function 'AssignX()' is a function of that class.
+ 1
that my point..
bro..
function only and only "be call in function only"
that the question..
+ 1
Jakob..
i find new about..this reference variable..
hope you read..
+ 1
Sorry, I think I'm having issue understanding your question.
You can call a function from within itself or outside of itself. When you call a function from inside of itself, that's called a recursive function, which creates a type of looping of itself to accomplish a task. Otherwise, you can call other functions inside of the function or you can call functions from outside of a function, such as you did when you assigned a function to the variable 'x.'
Question: "2.how can we call function at class level.."
^When you did "int x = AssignX();" - that's calling a function at class level.
+ 1
see this code again;
int x=AssignX();//<--
int AssignX ()
{
return 10;
}
void show()
{
System. out. println (x);//<--
}
only focuse"<--" mark...
above code..
in show function..
i only access x variable...
and
there is a rule
<---
Function can be called by function only...
so how its is possible...
+ 1
Anybody there to help me out to this problem
+ 1
https://www.sololearn.com/discuss/1199926/?ref=app
Jakob After a long research I find something..
hope you see and comment me..