Eclipse throwing error on invalid modifiers, rename in file - java

I'm a beginner in Java and I was typing this block of code in Eclipse and it is throwing errors like this. I haven't even started anything yet, but there's error with my variable name? I know Eclipse is very particular about duplicate variable names in maybe the same package or something. Is that maybe where the problem is?
Thanks!

You need to either declare those variables outside the main method (if you want them to have class scope), or remove the private keyword if you want them to have method scope, i.e. just in your main method.
So either this:
public class Person {
private String name;
// other variables...
public static void main(String[] arguments) {
// other code...
}
}
Or like this:
public class Person {
public static void main(String[] arguments) {
String name;
// other variables and code...
}
}

You can not use the access modifier private inside any method. Remove the access modifier private before the variable name.
Or you can declare these variable in class level (that is as instance variables) - outside of any methods. Since the name is a property/attribute of a Person, according to OOP it is better to keep the name as field of the Person class like this -
public class Person{
private String name;
//Other property of Person
public String getName(){
return name;
}
public String setName(String name){
this.name = name;
}
public static void main(String[] args){
}
}
Use public getter and setter method to access these private variable from outside of the Person class.

Either do this:
public class Person {
private String name; // Declared as an attribute of Person class
public static void main(String [] args) { ...}
}
Or this:
public class Person {
public static void main(String [] args) {
String name; // No private
// ...
}
}

Just remove the access modifier private in both the variables.Your problem
will be solved.You cannot declare private variables inside methods.

Related

Giving the object to create as parameta in constuctor

sorry for the title I don't know how to say that.
So basically what I want to do is the following:
public class TestClass {
private final String name;
public TestClass(String name) {
this.name = name;
}
public TestClass(TestClass test) {
this = test;
}
public String getName() {
return name;
}
}
So create a class where you can give an object from itself in the constructor
I don't know if that is possible at all.
The problem I have is that I have multiple objects that extend from this class and I want a simple way to pass them to the next class
so when I have 2 classes
TestClass2 extends TestClass
TestClass3 extends TestClass
and I want to create an instance of the testclass3 from the testclass2 one.
they both should have the same name from testcalss
currently, I am doing that like that:
private class TestClass2 extends TestClass{
private final String anotherName;
public TestClass2(String name, String anotherName) {
super(name);
this.anotherName= anotherName;
}
private void createTest3(String whatever) {
new TestClass3(this, whatever);
}
}
private class TestClass3 extends TestClass{
private final String whatever;
public TestClass3(TestClass test, String whatever) {
super(test.getName());
this.whatever = whatever;
}
}
In my case, my base class has not just a name but a lot more values that I then have to submit!
I hope you kinda understand what I want to say. Again sorry I explained that very bad :D
And thank you all thanks in advance for any answers!

how should i add an object to a private static ArrayList?

i have Bank class, with a private static ArrayList that stores all the banks. how can i add every new bank created to it?
i'm not allowed to create any new methods or fields, or change any of the method or constructor parameters.
this is my Bank class:
public class Bank {
private static ArrayList<Bank> allBanks=new ArrayList<Bank>();
private String name;
public Bank(String name) {
this.name = name;
}
public String getName() {
return name;
}
and this is my Main class:
public class Main {
public static void main(String[] args) {
new Bank("randomBankName");
}
}
Do it in constructor:
public Bank(String name) {
this.name = name;
allBanks.add(this);
}
WARNING never do it in real project.
You didn't say, that you may not change the visibility of fields, so that would be one way to do this: make the ArrayList public
If you may not do this either, there is a last way, which i'd never do: Reflection.
In most cases, thats really the last way, not recommended!

java - sending variables through nested classes

I have a class that has a variable of type Name.
public class Holder {
private Name name;
private int snumber;
The Name class has two strings called first and last that are assigned values by setter methods. I would like to send over the strings from the Name class to name in the Holder class, but I'm having trouble doing so. I think I've taken a step in the right direction by doing this
public class Holder {
private Name name;
private int snumber;
public void setName(){
name = new Name();
name.getFirst();
name.getLast();
}
but I can't say that I really know what the correct approach is. I also tried name.setFirst(getFirst) but that doesn't work. Any ideas would be appreciated.
The same way you would if the class wasn't nested.
Your setName() method should take a parameter (maybe 2, first and last) and then invoke the name.setFirstName(), name.setLastName() methods.
Right now, your setName() method isn't doing anything.
E.G:
public class Holder
{
private Name name;
private int snumber;
public Holder()
{
this.name = new Name();
}
public void setName(String firstName, String lastName)
{
this.name.setFirst(firstName);
this.name.setLAst(lastName);
}
}
Here is a good article explaining the relationship between Java inner and outer classes:
https://www.tutorialspoint.com/java/java_innerclasses.htm
class Outer_Demo {
// private variable of the outer class
private int num = 175;
// inner class
public class Inner_Demo {
public int getNum() {
System.out.println("This is the getnum method of the inner class");
return num;
}
}
}
public class My_class2 {
public static void main(String args[]) {
// Instantiating the outer class
Outer_Demo outer = new Outer_Demo();
// Instantiating the inner class
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
System.out.println(inner.getNum());
}
}
Note that the example creates instances of both "Outer_Demo" AND "Inner_Demo (outer.new Inner_Demo();).
Ok, so I figured something out that works.
public class Holder {
private int snumber;
private Name name;
public void setName(Name n){
name=n;
}
public Name getName(){
return name;
}

How to access private data member outside the class?

I am trying to access the private data member of inner class outside the outer class.
Please help me?
You don't - that's the whole point of it being private.
The inner class can expose the data via a property, of course:
public class Outer {
public class Inner {
private final String name;
public Inner(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}
public class Other {
public void foo() {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner("Foo");
// Can't access inner.name here...
System.out.println(inner.getName()); // But can call getName
}
}
... but if the inner class wants to keep it private, then you shouldn't try to access it.
Create public getter setter methods inside the inner class for private variables. Then create an object and call them to access private data. You can't directly access private data.
you cant access a private data. If ýou have other thoughts of accessing it use public getter method returning that private data.
you can access private data member using getter method ex
package pack;
import java.io.ObjectInputStream.GetField;
public class abc {
private int num = 2;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
class otherClass {
public static void main(String[] args) {
abc obj = new abc();
System.out.println(obj.getNum());
}
}

Initialize final variable before constructor in Java

Is there a solution to use a final variable in a Java constructor?
The problem is that if I initialize a final field like:
private final String name = "a name";
then I cannot use it in the constructor. Java first runs the constructor and then the fields. Is there a solution that allows me to access the final field in the constructor?
I do not really understand your question. That
public class Test3 {
private final String test = "test123";
public Test3() {
System.out.println("Test = "+test);
}
public static void main(String[] args) {
Test3 t = new Test3();
}
}
executes as follows:
$ javac Test3.java && java Test3
Test = test123
Do the initialization in the constructor, e.g.,
private final String name;
private YourObj() {
name = "a name";
}
Of course, if you actually know the value at variable declaration time, it makes more sense to make it a constant, e.g.,
private static final String NAME = "a name";
We're getting away from the question.
Yes, you can use a private final variable. For example:
public class Account {
private final String accountNumber;
private final String routingNumber;
public Account(String accountNumber, String routingNumber) {
this.accountNumber = accountNumber;
this.routingNumber = routingNumber;
}
}
What this means is that the Account class has a dependency on the two Strings, account and routing numbers. The values of these class attributes MUST be set when the Account class is constructed, and these number cannot be changed without creating a new class.
The 'final' modifier here makes the attributes immutable.
Marking it static, will allow you to use it in the constructor, but since you made it final, it can not be changed.
private static final String name = "a_name";
is is possible to use a static init block as well.
private static final String name;
static { name = "a_name"; }
If you are trying to modify the value in the constructor, then you can't assign a default value or you have to make it not final.
private String name = "a_name";
Foo( String name )
{
this.name = name;
}
or
private final String name;
Foo( String name )
{
if( s == null )
this.name = "a_name";
else
this.name = name;
}
In this case, you can mark the field as 'static' also.
Another possiblity is to initialize the field in an instance initializer blocK:
public class Foo {
final String bar;
{
System.out.println("initializing bar");
bar = "created at " + System.currentTimeMillis();
}
public Foo() {
System.out.println("in constructor. bar=" + bar);
}
public static void main(String[] args) {
new Foo();
}
}
In that case, you might as well make it static, too. And Java convention is to name such constants in ALL_CAPS.
private static final String name = getName();
where getName() is a static function that gets you the name.
I cannot use it in the constructor, while java first runs the constructor an then the fields...
This is not correct, fields are evaluated first, otherwise you couldn't access any default values of members in your constructors, since they would not be initialized. This does work:
public class A {
protected int member = 1;
public A() {
System.out.println(member);
}
}
The keyword final merely marks the member constant, it is treated as any other member otherwise.
EDIT: Are you trying to set the value in the constructor? That wouldn't work, since the member is immutable if defined as final.

Categories