answersLogoWhite

0


Best Answer

Yes perfectly Legal

Reason:

The scope of a variable declared inside a method is only till the method's end. So by defining another variable of the same name inside a different method does not create any issues.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

In Java there is a concept called Scope of a Variable. Which defines the places in the code where a variable can be accessed.

When you declare a variable inside a method, that variable is available only inside that method. Outside the method you cannot access it. So if you declare another variable of the same name in another method, it is as good as declaring a variable afresh because the instances of those variables are specific to that method.

Those two variables are not at all related.

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

Yes, but remember the scope of a variable defined in a method is limited to the method... so the values will not be interchangeable / accessible without being passed from one to another.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Method Overloading...method with same name co-exists in same class but they must have different method signature

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is it called when java allows you to declare methods with the same name in a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


What is the relationship between inheritance methods and the keyword final?

They are inversely related. That is: If you declare a method as final you cannot overridden in the child class If you declare a class as final you cannot inherit it in any other class.


What allows a new class to automatically pick up all the data and methods of an existing class?

Inheritance allows a new class to automatically pick up all the protected and public data and methods of an existing class. To do so, the new class must be derived from the existing class. Private data and methods remain private to the existing class, the base class.


What do you mean by classes within the classes?

You can declare and implement a class within a class...class abc {...class def {...}}The class def is scoped and known only within the context of abc. It is possible to declare instances of def as member variables of abc, however, for this to work correctly, they should be private, because methods of def, not even public methods, do not exist outside of the scope of abc.


The actions in a java class are called?

The actions in a java class are called methods.


Can additional methods of base class be hidden from the derived class in object oriented software?

Yes. During inheritance only the public members of the parent class are visible to the child class. If you declare a method or a variable as private, the child class cannot access it. In other words, the methods and variables are hidden from the derived class.


It allows the creation of a new class from a base class?

It is called Inheritance


What is the difference between a class method and an instance method?

Instance methods can be called by the object of a Class whereas static method are called by the Class. When objects of a Class are created, they have their own copy of instance methods and variables, stored in different memory locations. Static Methods and variables are shared among all the objects of the Class, stored in one fixed location in memory.Static methods cannotaccess instance variables or instance methods directly-they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.


How can you create a class which cannot be inheritable?

Declare it final.


When do you declare a member as static in java?

You declare a member static whenever the member should be regarded as being local to the class rather than being local to objects of the class. Static members are shared by all instances of the class. Static methods of a class differ from ordinary members in that they do not have an implicit "this" reference, which means they can be invoked even when no instances of the class exist.


Where does inheritance take place?

Inheritance, in the context of object-oriented programming, takes place between classes where one class (called the sub-class or child class) inherits attributes and methods from another class (called the super-class or parent class). This allows for code reusability and the establishment of a hierarchical relationship between classes.


Is it possible to call static methods with out creating object?

Yes, we can access all methods which declare with static means then we can access.. ex: class s{ static method() { System.out.println("Welcome"); } } class fun{ public static void main(String args[]) { method(); } }