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

static thing  RSS feed

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we cannt declare static variables in java methods .& static methods in interfaces ,but achieve their effect.
why that so .
we can have a method of same name as a class itself(not a constructor).Doesnt give any error.

sorry to ask such Basic Questions,but i am perplexed with so many possibilites - not possiblities.
 
Subhro Chowdhury
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lucky,

None of the methods can have same name as of the Class, if it is then it is a constructor. The arguments and accessability modifier can be differnt in multiple contructors, but the return type should always be void.

Static variables are class level variables like instance variables, the difference is, unlike instance variables, all objects of the corrosponding class will share same memory allocation for the static variables. Where as for the variables inside a method get a memory when the control is in the scope. After that those are destroyed. So static variables are in contrast to the variables with in a method.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Subhro Chowdhury:

None of the methods can have same name as of the Class, if it is then it is a constructor.


Try compiling this

Originally posted by Subhro Chowdhury:

The arguments and accessability modifier can be differnt in multiple contructors, but the return type should always be void.


Constructors don't have return types.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried these programs ,what i concluded
is
Methods can have same name as Class ,can accept arguments
& need to be called explicitly like normal methods
But return-type must be void always .Cannot int int ..etc

These are not constructors ,constructors mplicit return type is the ClassName itself.

I might be wrong ,If it is ,please correct me.
I tried to make the method STATIC ,No comiplation error but Runtime
Thnkx
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lucky J Verma:
But return-type must be void always .Cannot int int ..etc


Did you change the signature of the method ? The return type is not part of the signature, but parameters are, so
will not compile, butwill.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i tried changing the parameters ,it works all the ways as a normal methods ,but ret-type must be void .
we can make it static also.
it is accessible in the main class
Test.Test(); //if Test() is a static method in Test class


Thank you
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think the return type has to be void ? Can you post some code to demonstrate this.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i tried changing the parameters ,it works all the ways as a normal methods ,but ret-type must be void .


Did you have some return statement in your code?
When you have a non-void return type, you must have a return statement to able to compile.

Bart.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!