Jump to content

Java classes,objects and variables

Go to solution Solved by Slottr,
8 minutes ago, stefanmz said:

no I meant

class Foo
{
	variable=value
	foo(variable){}
}

Foo obj=new Foo(variable);

why does it say variable does not exist?

 

- Variable isnt local to where you are creating obj

- Variable will need to be public to be accessible AFTER you have created the class

- You need to consider using a constructor upon class creation to set values

Ok so if I have objects of a class that are public(defined outside of the class) and they use variables that are defined in the class why does it say variables doesn't exist? Do they have to be public? If the object and variables are of the same class can't the object use the variables even though it's outside of the class and they(the variables) are in the class?

 

Link to comment
https://linustechtips.com/topic/1393015-java-classesobjects-and-variables/
Share on other sites

Link to post
Share on other sites

Can you post your code so I can get a better idea of what you're referencing?

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 9600X || GPU: RX 9070 XT|| Memory: 32GB || Cooler: Peerless Assassin || PSU: RM850e|| Case: Lian Li A3

Link to post
Share on other sites

I am not sure I understand your question 100% but I'll take shot but if I am off you might need to provide so better/clearer info.

 

Access modifiers define the access permissions of attributes and methods/functions that are contained within the class. The public keyword makes it so that everything can access that attribute or function that follow the modifier. Protected makes it so that its visible to only the package and child/sub classes. Private states that its only an visible to that class only. No modifier makes it visible to the package only. 

 

Like so but excluding no modifier and protected. These access modifiers are how you build the interface (not speaking java interfaces but just how you interact with the class) for a class and encapsulate data.

class foo 
{
	public int attribute1;
    private int attribute2;
    
    public int getAttribute2() { return attribute 2} 
    public void setAttribute2(int att) { attribute2 = att} // note a class instance can access its own private members and functions without any issue
    private void bar() { /// }
}

// outside code
Foo f = new Foo();
f.attribute1 = 1; // Legal, because its public
f.attribute2 = 1; // Illegal because its private has to be accessed through getters and setters
f.setAttribute2(2); // Legal because its public. Attribute 2 will now have the value of 2.
f.bar(); // Illegal because bar() is not public so it cannot be accessed outside of the class

 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

17 minutes ago, trag1c said:

I am not sure I understand your question 100% but I'll take shot but if I am off you might need to provide so better/clearer info.

 

Access modifiers define the access permissions of attributes and methods/functions that are contained within the class. The public keyword makes it so that everything can access that attribute or function that follow the modifier. Protected makes it so that its visible to only the package and child/sub classes. Private states that its only an visible to that class only. No modifier makes it visible to the package only. 

 

Like so but excluding no modifier and protected. These access modifiers are how you build the interface (not speaking java interfaces but just how you interact with the class) for a class and encapsulate data.

class foo 
{
	public int attribute1;
    private int attribute2;
    
    public int getAttribute2() { return attribute 2} 
    public void setAttribute2(int att) { attribute2 = att} // note a class instance can access its own private members and functions without any issue
    private void bar() { /// }
}

// outside code
Foo f = new Foo();
f.attribute1 = 1; // Legal, because its public
f.attribute2 = 1; // Illegal because its private has to be accessed through getters and setters
f.setAttribute2(2); // Legal because its public. Attribute 2 will now have the value of 2.
f.bar(); // Illegal because bar() is not public so it cannot be accessed outside of the class

 

no I meant

class Foo
{
	variable=value
	foo(variable){}
}

Foo obj=new Foo(variable);

why does it say variable does not exist?

Link to post
Share on other sites

8 minutes ago, stefanmz said:

no I meant

class Foo
{
	variable=value
	foo(variable){}
}

Foo obj=new Foo(variable);

why does it say variable does not exist?

 

- Variable isnt local to where you are creating obj

- Variable will need to be public to be accessible AFTER you have created the class

- You need to consider using a constructor upon class creation to set values

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 9600X || GPU: RX 9070 XT|| Memory: 32GB || Cooler: Peerless Assassin || PSU: RM850e|| Case: Lian Li A3

Link to post
Share on other sites

2 minutes ago, Slottr said:

 

- Variable isnt local to where you are creating obj

- Variable will need to be public to be accessible AFTER you have created the class

- You need to consider using a constructor upon class creation to set values

ok thanks!

Link to post
Share on other sites

  • 1 month later...

Class

A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods common to all objects of one type. In general, class declarations can include these components, in order: 

  1. Modifiers: A class can be public or has default access.
  2. Class keyword: class keyword is used to create a class.
  3. Class name: The name should begin with an initial letter (capitalized by convention).
  4. Superclass(if any): The name of the class’s parent (superclass), preceded by the keyword, extends. A class can only extend (subclass) one parent.
  5. Interfaces(if any): A comma-separated list of interfaces implemented by the class, preceded by the keyword implements. A class can implement more than one interface.
  6. Body: The class body surrounded by braces, { }.

Object

It is a basic unit of Object-Oriented Programming and represents real-life entities. A typical Java program creates many objects, which, as you know, interact by invoking methods. An object consists of : 

  1. State: It is represented by attributes of an object. It also reflects the properties of an object.
  2. Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.
  3. Identity: It gives an object a unique name and enables one to interact with other objects.

Variables

 

variables in Java are data containers that save the data values during Java program execution. Every variable is assigned a data type that designates the type and quantity of value it can hold. Variable is a memory location name of the data.

A variable is a name given to a memory location. It is the basic unit of storage in a program.

  • The value stored in a variable can be changed during program execution.
  • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
  • In Java, all the variables must be declared before use.

Several organizations offer online java training, but Edureka has been rapidly growing with its online presence in the last couple of years. It helps you learn popular frameworks like Hibernate, java Array, Java Functions & Java Loops, etc. certifies you as an SOA & Java Developer based on the project reviewed by our expert panel to enhance your career.

Link to post
Share on other sites

On 12/3/2021 at 3:09 AM, stefanmz said:

no I meant

class Foo
{
	variable=value
	foo(variable){}
}

Foo obj=new Foo(variable);

why does it say variable does not exist?

 

To turn this into valid Java code, it would have to look something like this:

// Class should be public, so others can create an instance
public class Foo {
  
  // <type> is something like int, float, String, … or some other class
  // field should be private
  // final if the field can only be set in the constructor and not changed afterwards
  private [final] <type> variable;
  
  // Constructor that assigns the field
  public Foo(final <type> variable) {
    this.variable = variable;
  }
}

// Now you can create an instance of type Foo somewhere else
Foo obj = new Foo(<value>);

 

Your example is missing types for your variables (which is why it "doesn't exist", you haven't properly declared it), your constructor doesn't match the class name ("Foo", not "foo") and "variable=value" doesn't make sense here. You're outside a method body, variable hasn't been declared (no type) and where is "value" supposed to be coming from?

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×