Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Aurélie MEIJER
Java Web Development Techdegree Student 761 PointsSimple Java questions
Hi everybody,
I have some Java questions that keeps bugging me and i need your help guys.
Question 1:
Is this command : String jacksons = new String; is equal to this: String jacksons;
Question 2:
Let's assume, we have 2 classes : Class1 and Class2 under the same package com.teamtreehouse and we assume there is a method in Class1 under the name method1.
is the command : import com.teamtreehouse.Class1; Class1.method1;
Equals to: Class1 test= new Class1(); Class1.method1;
if not what cases are they similar, i need you guys to be clear as possible. I'm very dumb hahaa . Thank you for help.
2 Answers
Simon Coates
28,695 PointsQuestion 1: Is this command : String jacksons = new String; is equal to this: String jacksons; No, it isn't. String jacksons; just means that there is a variable called jacksons that WILL hold a string. This is called 'declaration'. To declare and assign a string you could use something like:
String jacksons = new String("this is a string"); //standard object construction.
or
String jacksons = "this is a string"; //string while being an object has special support in java.
Simon Coates
28,695 PointsQuestion 2: the statement
import com.teamtreehouse.Class1;
should give you access to Class1. (I'm not 100% sure as i don't use java, but you may have access to classes in the same package without import. not sure ). But with an object method (as opposed to a static method), you will still need to create an instance of an object, and call/invoke the method on that object. For example, something like.
Class1 test= new Class1();
test.method1();
By contrast, if you see something like:
Class1.someMethod();
this is likely a static method. (doesn't require an object, but is called directly on the class.)
Simon Coates
28,695 Pointsdeclaring a variable, without assigning a value can sometimes be useful. You probably don't need to understand this yet, but i've tried to explain it. Variables in java have 'scope' - they exist inside the {} that encloses them. for example:
public class SomeClass {
public String instanceMember = "someValue"; //is available anywhere in the class for the lifetime of the object.
public void method(){
String greeting; //the enclosing {} are the method - greeting exists within and for the duration of the method.
bool isMorning = false;
if(isMorning)
{
greeting = "Good Morning";
String x = "x only exist within the {} in which it is defined."; //the enclosing {} is the if.
} else {
greeting = "Good Evening";
}
System.out.println(greeting);
//System.out.println(x); // this would crash as x doesn't exist here. Hence this line is commented out.
}
}
A variable is just a named container. The scope defines where you are able to make reference to the name. The variable value may continue to exist beyond the scope of the original variable - if it is assigned to another variable that continues to exist or returned by a method. A value disappears (java has a "garbage collector") when there is no variable left that contains/makes reference to it. (a related topic that affects whether values continue to exist is pass by reference and pass by value - behaviour differs if using object or primitive types).
Simon Coates
28,695 PointsSimon Coates
28,695 Pointsi attempted to answer your questions. I may have talked about stuff (scope, variable lifetime, pass by value, pass by reference) that will be covered in the java techdegree, but you may not have covered yet. As such, you may not need to understand everything i wrote - at least not yet.