Get file name from TUI class to different class [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Currently working on a tui application to prompt the user to enter a file name and then I need to get that name from the TUI class over to a model class. Every thing I try gives me the error "Cannot make a static reference to the non-static method getFileName() from the type TUI." Any suggestions on how I should go about the code? I know I haven't supplied a ton of information but I'm not completely sure what information is pertinent.

The problem is that your method getFileName() is not static, so it belongs to an object and you're trying to access it though the class and not an object. If it's the same fileName for every TUI object you just need to make the field fileName and the method getFileName() static.
public static void getFileName() {
If not you have to call the method on an object with the dot operator
TUI tui= new TUI();
TUI.getFileName();

Related

polymorpishm in java abstract class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
We cannot create objects from abstract classes in Java. But there is a part that I do not understand. I have an abstract class called "GameCharacter" in my project. A resource told me to write the code in my assignment like this, but I don't understand, did we not use an object in parentheses(GameCharacter gamechar) Didn't we use an object called gameChar in ?
public void setPlayer(GameCharacter gamechar){
}
This is where inheritance comes into play: gamechar can be any instance of GameCharacter, or any class that extends it. You may not be able to directly create a GameCharacter instance, but this method's logic should work with any instance of any concrete class that extends it.

When to use public modifier for a class field in OOP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
When programming in OO languages like C# or Java, is there a good situation where declaring a public field inside a class is actually valid (I myself always use a property for not making the user of the class depend on the data and to support data protection)?
Otherwise, it feels weird that C# for example allows you to do so.
According to the C# coding conventions public field should be used sparingly:
// A public field, these should be used sparingly
public bool IsValid;
Why? I think because of:
can be edited by any other user of class
if you want to add some logic to field, then you need to create property instead of field. By doing this, you will break a contract of class
it is not possible to override variable
However, there is a case when you need to have field as #VGR said:
public const string foo = "";

How can multiple classes call one method that takes an instance of the calling class as a parameter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want all my other objects to be able to call one common method. What should my shared method type signature for parameters look like?
public class appSocket{
public appSocket appSocket(TYPE genericInstance){
// Do something with genericInstance here
}
}
In other words, what should "TYPE" look like?
I think you're a bit confused. This just corresponds to the current object. There is no reason to ever say someObject.this.foo(). You'd just say someObject.foo(). You just pass in the object.

why static methods and variables are accessible without any instance of class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why is it that all static methods and variables are accessible without any instance of class and non-static members need instances to get access.
Actually, there is a class object representing each class in the JVM. So, the line Why is it that all static methods and variables are accessible without any instance of class is incorrect.
The JVM creates class objects (different from class instances) representing classes.
Example : String.class, Class.class etc
When We create a class that means we are creating 'Bunch of Objects(instances) of Same type',for those objects,methods are remains same but data and memory location changes and it is unique for each object.But When We use 'Static' variable or Method ,It creates Only ONE COMMON COPY in whole program.So it is same for all instances/objects and changes done in static method/variable is visible to all objects. hence,we can use it directly or without instance of class.in same way,non static members are different with respect to every object,so we need instance of class for it.

Must write a java class constructor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have been asked in a interview. It seems there is a scenario when, a constructor must need to write while writing a java class. I could not find a proper answer though. Please help me.
Cheers
You must write a constructor when your base class does not have a standard one, which takes no argument.
For example:
class A {
public A(int value) {
}
}
class B extends A {
}
This code does not compile. Java will try to add a default constructor to B, but it could not call super(), since A does not have a zero-argument constructor. Thus, you must define a constructor for B.
If you extend a class that doesn't have a parameterless constructor you must define a constructor.

Categories

Resources